/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaObjCProperty.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for Objective C @property and |
10 | | // @synthesize declarations. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Sema/SemaInternal.h" |
15 | | #include "clang/AST/ASTMutationListener.h" |
16 | | #include "clang/AST/DeclObjC.h" |
17 | | #include "clang/AST/ExprCXX.h" |
18 | | #include "clang/AST/ExprObjC.h" |
19 | | #include "clang/Basic/SourceManager.h" |
20 | | #include "clang/Lex/Lexer.h" |
21 | | #include "clang/Lex/Preprocessor.h" |
22 | | #include "clang/Sema/Initialization.h" |
23 | | #include "llvm/ADT/DenseSet.h" |
24 | | #include "llvm/ADT/SmallString.h" |
25 | | |
26 | | using namespace clang; |
27 | | |
28 | | //===----------------------------------------------------------------------===// |
29 | | // Grammar actions. |
30 | | //===----------------------------------------------------------------------===// |
31 | | |
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 |
39 | 1.29k | getImpliedARCOwnership(ObjCPropertyAttribute::Kind attrs, QualType type) { |
40 | | // retain, strong, copy, weak, and unsafe_unretained are only legal |
41 | | // on properties of retainable pointer type. |
42 | 1.29k | if (attrs & |
43 | 1.29k | (ObjCPropertyAttribute::kind_retain | ObjCPropertyAttribute::kind_strong | |
44 | 1.29k | ObjCPropertyAttribute::kind_copy)) { |
45 | 700 | return Qualifiers::OCL_Strong; |
46 | 700 | } else if (593 attrs & ObjCPropertyAttribute::kind_weak593 ) { |
47 | 205 | return Qualifiers::OCL_Weak; |
48 | 388 | } else if (attrs & ObjCPropertyAttribute::kind_unsafe_unretained) { |
49 | 317 | return Qualifiers::OCL_ExplicitNone; |
50 | 317 | } |
51 | | |
52 | | // assign can appear on other types, so we have to check the |
53 | | // property type. |
54 | 71 | if (attrs & ObjCPropertyAttribute::kind_assign && |
55 | 71 | type->isObjCRetainableType()0 ) { |
56 | 0 | return Qualifiers::OCL_ExplicitNone; |
57 | 0 | } |
58 | | |
59 | 71 | return Qualifiers::OCL_None; |
60 | 71 | } |
61 | | |
62 | | /// Check the internal consistency of a property declaration with |
63 | | /// an explicit ownership qualifier. |
64 | | static void checkPropertyDeclWithOwnership(Sema &S, |
65 | 164 | ObjCPropertyDecl *property) { |
66 | 164 | if (property->isInvalidDecl()) return0 ; |
67 | | |
68 | 164 | ObjCPropertyAttribute::Kind propertyKind = property->getPropertyAttributes(); |
69 | 164 | Qualifiers::ObjCLifetime propertyLifetime |
70 | 164 | = property->getType().getObjCLifetime(); |
71 | | |
72 | 164 | assert(propertyLifetime != Qualifiers::OCL_None); |
73 | | |
74 | 0 | Qualifiers::ObjCLifetime expectedLifetime |
75 | 164 | = getImpliedARCOwnership(propertyKind, property->getType()); |
76 | 164 | if (!expectedLifetime) { |
77 | | // We have a lifetime qualifier but no dominating property |
78 | | // attribute. That's okay, but restore reasonable invariants by |
79 | | // setting the property attribute according to the lifetime |
80 | | // qualifier. |
81 | 0 | ObjCPropertyAttribute::Kind attr; |
82 | 0 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
83 | 0 | attr = ObjCPropertyAttribute::kind_strong; |
84 | 0 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
85 | 0 | attr = ObjCPropertyAttribute::kind_weak; |
86 | 0 | } else { |
87 | 0 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
88 | 0 | attr = ObjCPropertyAttribute::kind_unsafe_unretained; |
89 | 0 | } |
90 | 0 | property->setPropertyAttributes(attr); |
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | 164 | if (propertyLifetime == expectedLifetime) return43 ; |
95 | | |
96 | 121 | property->setInvalidDecl(); |
97 | 121 | S.Diag(property->getLocation(), |
98 | 121 | diag::err_arc_inconsistent_property_ownership) |
99 | 121 | << property->getDeclName() |
100 | 121 | << expectedLifetime |
101 | 121 | << propertyLifetime; |
102 | 121 | } |
103 | | |
104 | | /// Check this Objective-C property against a property declared in the |
105 | | /// given protocol. |
106 | | static void |
107 | | CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop, |
108 | | ObjCProtocolDecl *Proto, |
109 | 340k | llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) { |
110 | | // Have we seen this protocol before? |
111 | 340k | if (!Known.insert(Proto).second) |
112 | 3.78k | return; |
113 | | |
114 | | // Look for a property with the same name. |
115 | 336k | if (ObjCPropertyDecl *ProtoProp = Proto->getProperty( |
116 | 336k | Prop->getIdentifier(), Prop->isInstanceProperty())) { |
117 | 1.40k | S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true); |
118 | 1.40k | return; |
119 | 1.40k | } |
120 | | |
121 | | // Check this property against any protocols we inherit. |
122 | 335k | for (auto *P : Proto->protocols()) |
123 | 112k | CheckPropertyAgainstProtocol(S, Prop, P, Known); |
124 | 335k | } |
125 | | |
126 | 117k | static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) { |
127 | | // In GC mode, just look for the __weak qualifier. |
128 | 117k | if (S.getLangOpts().getGC() != LangOptions::NonGC) { |
129 | 29 | if (T.isObjCGCWeak()) |
130 | 0 | return ObjCPropertyAttribute::kind_weak; |
131 | | |
132 | | // In ARC/MRC, look for an explicit ownership qualifier. |
133 | | // For some reason, this only applies to __weak. |
134 | 117k | } else if (auto ownership = T.getObjCLifetime()) { |
135 | 36 | switch (ownership) { |
136 | 33 | case Qualifiers::OCL_Weak: |
137 | 33 | return ObjCPropertyAttribute::kind_weak; |
138 | 3 | case Qualifiers::OCL_Strong: |
139 | 3 | return ObjCPropertyAttribute::kind_strong; |
140 | 0 | case Qualifiers::OCL_ExplicitNone: |
141 | 0 | return ObjCPropertyAttribute::kind_unsafe_unretained; |
142 | 0 | case Qualifiers::OCL_Autoreleasing: |
143 | 0 | case Qualifiers::OCL_None: |
144 | 0 | return 0; |
145 | 36 | } |
146 | 0 | llvm_unreachable("bad qualifier"); |
147 | 0 | } |
148 | | |
149 | 117k | return 0; |
150 | 117k | } |
151 | | |
152 | | static const unsigned OwnershipMask = |
153 | | (ObjCPropertyAttribute::kind_assign | ObjCPropertyAttribute::kind_retain | |
154 | | ObjCPropertyAttribute::kind_copy | ObjCPropertyAttribute::kind_weak | |
155 | | ObjCPropertyAttribute::kind_strong | |
156 | | ObjCPropertyAttribute::kind_unsafe_unretained); |
157 | | |
158 | 1.05M | static unsigned getOwnershipRule(unsigned attr) { |
159 | 1.05M | unsigned result = attr & OwnershipMask; |
160 | | |
161 | | // From an ownership perspective, assign and unsafe_unretained are |
162 | | // identical; make sure one also implies the other. |
163 | 1.05M | if (result & (ObjCPropertyAttribute::kind_assign | |
164 | 1.05M | ObjCPropertyAttribute::kind_unsafe_unretained)) { |
165 | 13.8k | result |= ObjCPropertyAttribute::kind_assign | |
166 | 13.8k | ObjCPropertyAttribute::kind_unsafe_unretained; |
167 | 13.8k | } |
168 | | |
169 | 1.05M | return result; |
170 | 1.05M | } |
171 | | |
172 | | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
173 | | SourceLocation LParenLoc, |
174 | | FieldDeclarator &FD, |
175 | | ObjCDeclSpec &ODS, |
176 | | Selector GetterSel, |
177 | | Selector SetterSel, |
178 | | tok::ObjCKeywordKind MethodImplKind, |
179 | 351k | DeclContext *lexicalDC) { |
180 | 351k | unsigned Attributes = ODS.getPropertyAttributes(); |
181 | 351k | FD.D.setObjCWeakProperty((Attributes & ObjCPropertyAttribute::kind_weak) != |
182 | 351k | 0); |
183 | 351k | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
184 | 351k | QualType T = TSI->getType(); |
185 | 351k | if (!getOwnershipRule(Attributes)) { |
186 | 117k | Attributes |= deducePropertyOwnershipFromType(*this, T); |
187 | 117k | } |
188 | 351k | bool isReadWrite = ((Attributes & ObjCPropertyAttribute::kind_readwrite) || |
189 | | // default is readwrite! |
190 | 351k | !(Attributes & ObjCPropertyAttribute::kind_readonly)350k ); |
191 | | |
192 | | // Proceed with constructing the ObjCPropertyDecls. |
193 | 351k | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
194 | 351k | ObjCPropertyDecl *Res = nullptr; |
195 | 351k | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
196 | 48.6k | if (CDecl->IsClassExtension()) { |
197 | 595 | Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, |
198 | 595 | FD, |
199 | 595 | GetterSel, ODS.getGetterNameLoc(), |
200 | 595 | SetterSel, ODS.getSetterNameLoc(), |
201 | 595 | isReadWrite, Attributes, |
202 | 595 | ODS.getPropertyAttributes(), |
203 | 595 | T, TSI, MethodImplKind); |
204 | 595 | if (!Res) |
205 | 12 | return nullptr; |
206 | 595 | } |
207 | 48.6k | } |
208 | | |
209 | 351k | if (!Res) { |
210 | 351k | Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD, |
211 | 351k | GetterSel, ODS.getGetterNameLoc(), SetterSel, |
212 | 351k | ODS.getSetterNameLoc(), isReadWrite, Attributes, |
213 | 351k | ODS.getPropertyAttributes(), T, TSI, |
214 | 351k | MethodImplKind); |
215 | 351k | if (lexicalDC) |
216 | 0 | Res->setLexicalDeclContext(lexicalDC); |
217 | 351k | } |
218 | | |
219 | | // Validate the attributes on the @property. |
220 | 351k | CheckObjCPropertyAttributes(Res, AtLoc, Attributes, |
221 | 351k | (isa<ObjCInterfaceDecl>(ClassDecl) || |
222 | 351k | isa<ObjCProtocolDecl>(ClassDecl)54.1k )); |
223 | | |
224 | | // Check consistency if the type has explicit ownership qualification. |
225 | 351k | if (Res->getType().getObjCLifetime()) |
226 | 164 | checkPropertyDeclWithOwnership(*this, Res); |
227 | | |
228 | 351k | llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos; |
229 | 351k | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
230 | | // For a class, compare the property against a property in our superclass. |
231 | 297k | bool FoundInSuper = false; |
232 | 297k | ObjCInterfaceDecl *CurrentInterfaceDecl = IFace; |
233 | 747k | while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) { |
234 | 452k | if (ObjCPropertyDecl *SuperProp = Super->getProperty( |
235 | 452k | Res->getIdentifier(), Res->isInstanceProperty())) { |
236 | 2.79k | DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false); |
237 | 2.79k | FoundInSuper = true; |
238 | 2.79k | break; |
239 | 2.79k | } |
240 | 449k | CurrentInterfaceDecl = Super; |
241 | 449k | } |
242 | | |
243 | 297k | if (FoundInSuper) { |
244 | | // Also compare the property against a property in our protocols. |
245 | 2.79k | for (auto *P : CurrentInterfaceDecl->protocols()) { |
246 | 123 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
247 | 123 | } |
248 | 294k | } else { |
249 | | // Slower path: look in all protocols we referenced. |
250 | 294k | for (auto *P : IFace->all_referenced_protocols()) { |
251 | 223k | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
252 | 223k | } |
253 | 294k | } |
254 | 297k | } else if (ObjCCategoryDecl *54.1k Cat54.1k = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
255 | | // We don't check if class extension. Because properties in class extension |
256 | | // are meant to override some of the attributes and checking has already done |
257 | | // when property in class extension is constructed. |
258 | 48.6k | if (!Cat->IsClassExtension()) |
259 | 48.0k | for (auto *P : Cat->protocols()) |
260 | 92 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
261 | 48.6k | } else { |
262 | 5.53k | ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl); |
263 | 5.53k | for (auto *P : Proto->protocols()) |
264 | 3.91k | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
265 | 5.53k | } |
266 | | |
267 | 351k | ActOnDocumentableDecl(Res); |
268 | 351k | return Res; |
269 | 351k | } |
270 | | |
271 | | static ObjCPropertyAttribute::Kind |
272 | 351k | makePropertyAttributesAsWritten(unsigned Attributes) { |
273 | 351k | unsigned attributesAsWritten = 0; |
274 | 351k | if (Attributes & ObjCPropertyAttribute::kind_readonly) |
275 | 234k | attributesAsWritten |= ObjCPropertyAttribute::kind_readonly; |
276 | 351k | if (Attributes & ObjCPropertyAttribute::kind_readwrite) |
277 | 1.33k | attributesAsWritten |= ObjCPropertyAttribute::kind_readwrite; |
278 | 351k | if (Attributes & ObjCPropertyAttribute::kind_getter) |
279 | 16.9k | attributesAsWritten |= ObjCPropertyAttribute::kind_getter; |
280 | 351k | if (Attributes & ObjCPropertyAttribute::kind_setter) |
281 | 89 | attributesAsWritten |= ObjCPropertyAttribute::kind_setter; |
282 | 351k | if (Attributes & ObjCPropertyAttribute::kind_assign) |
283 | 6.03k | attributesAsWritten |= ObjCPropertyAttribute::kind_assign; |
284 | 351k | if (Attributes & ObjCPropertyAttribute::kind_retain) |
285 | 20.0k | attributesAsWritten |= ObjCPropertyAttribute::kind_retain; |
286 | 351k | if (Attributes & ObjCPropertyAttribute::kind_strong) |
287 | 12.1k | attributesAsWritten |= ObjCPropertyAttribute::kind_strong; |
288 | 351k | if (Attributes & ObjCPropertyAttribute::kind_weak) |
289 | 1.04k | attributesAsWritten |= ObjCPropertyAttribute::kind_weak; |
290 | 351k | if (Attributes & ObjCPropertyAttribute::kind_copy) |
291 | 195k | attributesAsWritten |= ObjCPropertyAttribute::kind_copy; |
292 | 351k | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
293 | 44 | attributesAsWritten |= ObjCPropertyAttribute::kind_unsafe_unretained; |
294 | 351k | if (Attributes & ObjCPropertyAttribute::kind_nonatomic) |
295 | 4.83k | attributesAsWritten |= ObjCPropertyAttribute::kind_nonatomic; |
296 | 351k | if (Attributes & ObjCPropertyAttribute::kind_atomic) |
297 | 2.57k | attributesAsWritten |= ObjCPropertyAttribute::kind_atomic; |
298 | 351k | if (Attributes & ObjCPropertyAttribute::kind_class) |
299 | 65.2k | attributesAsWritten |= ObjCPropertyAttribute::kind_class; |
300 | 351k | if (Attributes & ObjCPropertyAttribute::kind_direct) |
301 | 52 | attributesAsWritten |= ObjCPropertyAttribute::kind_direct; |
302 | | |
303 | 351k | return (ObjCPropertyAttribute::Kind)attributesAsWritten; |
304 | 351k | } |
305 | | |
306 | | static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, |
307 | 8 | SourceLocation LParenLoc, SourceLocation &Loc) { |
308 | 8 | if (LParenLoc.isMacroID()) |
309 | 0 | return false; |
310 | | |
311 | 8 | SourceManager &SM = Context.getSourceManager(); |
312 | 8 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc); |
313 | | // Try to load the file buffer. |
314 | 8 | bool invalidTemp = false; |
315 | 8 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
316 | 8 | if (invalidTemp) |
317 | 0 | return false; |
318 | 8 | const char *tokenBegin = file.data() + locInfo.second; |
319 | | |
320 | | // Lex from the start of the given location. |
321 | 8 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), |
322 | 8 | Context.getLangOpts(), |
323 | 8 | file.begin(), tokenBegin, file.end()); |
324 | 8 | Token Tok; |
325 | 34 | do { |
326 | 34 | lexer.LexFromRawLexer(Tok); |
327 | 34 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName16 ) { |
328 | 6 | Loc = Tok.getLocation(); |
329 | 6 | return true; |
330 | 6 | } |
331 | 34 | } while (Tok.isNot(tok::r_paren)28 ); |
332 | 2 | return false; |
333 | 8 | } |
334 | | |
335 | | /// Check for a mismatch in the atomicity of the given properties. |
336 | | static void checkAtomicPropertyMismatch(Sema &S, |
337 | | ObjCPropertyDecl *OldProperty, |
338 | | ObjCPropertyDecl *NewProperty, |
339 | 4.32k | bool PropagateAtomicity) { |
340 | | // If the atomicity of both matches, we're done. |
341 | 4.32k | bool OldIsAtomic = (OldProperty->getPropertyAttributes() & |
342 | 4.32k | ObjCPropertyAttribute::kind_nonatomic) == 0; |
343 | 4.32k | bool NewIsAtomic = (NewProperty->getPropertyAttributes() & |
344 | 4.32k | ObjCPropertyAttribute::kind_nonatomic) == 0; |
345 | 4.32k | if (OldIsAtomic == NewIsAtomic) return4.30k ; |
346 | | |
347 | | // Determine whether the given property is readonly and implicitly |
348 | | // atomic. |
349 | 11 | auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool { |
350 | | // Is it readonly? |
351 | 8 | auto Attrs = Property->getPropertyAttributes(); |
352 | 8 | if ((Attrs & ObjCPropertyAttribute::kind_readonly) == 0) |
353 | 3 | return false; |
354 | | |
355 | | // Is it nonatomic? |
356 | 5 | if (Attrs & ObjCPropertyAttribute::kind_nonatomic) |
357 | 0 | return false; |
358 | | |
359 | | // Was 'atomic' specified directly? |
360 | 5 | if (Property->getPropertyAttributesAsWritten() & |
361 | 5 | ObjCPropertyAttribute::kind_atomic) |
362 | 3 | return false; |
363 | | |
364 | 2 | return true; |
365 | 5 | }; |
366 | | |
367 | | // If we're allowed to propagate atomicity, and the new property did |
368 | | // not specify atomicity at all, propagate. |
369 | 11 | const unsigned AtomicityMask = (ObjCPropertyAttribute::kind_atomic | |
370 | 11 | ObjCPropertyAttribute::kind_nonatomic); |
371 | 11 | if (PropagateAtomicity && |
372 | 11 | ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)8 ) { |
373 | 3 | unsigned Attrs = NewProperty->getPropertyAttributes(); |
374 | 3 | Attrs = Attrs & ~AtomicityMask; |
375 | 3 | if (OldIsAtomic) |
376 | 0 | Attrs |= ObjCPropertyAttribute::kind_atomic; |
377 | 3 | else |
378 | 3 | Attrs |= ObjCPropertyAttribute::kind_nonatomic; |
379 | | |
380 | 3 | NewProperty->overwritePropertyAttributes(Attrs); |
381 | 3 | return; |
382 | 3 | } |
383 | | |
384 | | // One of the properties is atomic; if it's a readonly property, and |
385 | | // 'atomic' wasn't explicitly specified, we're okay. |
386 | 8 | if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)6 ) || |
387 | 8 | (6 NewIsAtomic6 && isImplicitlyReadonlyAtomic(NewProperty)2 )) |
388 | 2 | return; |
389 | | |
390 | | // Diagnose the conflict. |
391 | 6 | const IdentifierInfo *OldContextName; |
392 | 6 | auto *OldDC = OldProperty->getDeclContext(); |
393 | 6 | if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC)) |
394 | 0 | OldContextName = Category->getClassInterface()->getIdentifier(); |
395 | 6 | else |
396 | 6 | OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier(); |
397 | | |
398 | 6 | S.Diag(NewProperty->getLocation(), diag::warn_property_attribute) |
399 | 6 | << NewProperty->getDeclName() << "atomic" |
400 | 6 | << OldContextName; |
401 | 6 | S.Diag(OldProperty->getLocation(), diag::note_property_declare); |
402 | 6 | } |
403 | | |
404 | | ObjCPropertyDecl * |
405 | | Sema::HandlePropertyInClassExtension(Scope *S, |
406 | | SourceLocation AtLoc, |
407 | | SourceLocation LParenLoc, |
408 | | FieldDeclarator &FD, |
409 | | Selector GetterSel, |
410 | | SourceLocation GetterNameLoc, |
411 | | Selector SetterSel, |
412 | | SourceLocation SetterNameLoc, |
413 | | const bool isReadWrite, |
414 | | unsigned &Attributes, |
415 | | const unsigned AttributesAsWritten, |
416 | | QualType T, |
417 | | TypeSourceInfo *TSI, |
418 | 595 | tok::ObjCKeywordKind MethodImplKind) { |
419 | 595 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); |
420 | | // Diagnose if this property is already in continuation class. |
421 | 595 | DeclContext *DC = CurContext; |
422 | 595 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
423 | 595 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
424 | | |
425 | | // We need to look in the @interface to see if the @property was |
426 | | // already declared. |
427 | 595 | if (!CCPrimary) { |
428 | 0 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
429 | 0 | return nullptr; |
430 | 0 | } |
431 | | |
432 | 595 | bool isClassProperty = |
433 | 595 | (AttributesAsWritten & ObjCPropertyAttribute::kind_class) || |
434 | 595 | (Attributes & ObjCPropertyAttribute::kind_class)594 ; |
435 | | |
436 | | // Find the property in the extended class's primary class or |
437 | | // extensions. |
438 | 595 | ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass( |
439 | 595 | PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty)); |
440 | | |
441 | | // If we found a property in an extension, complain. |
442 | 595 | if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())137 ) { |
443 | 4 | Diag(AtLoc, diag::err_duplicate_property); |
444 | 4 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
445 | 4 | return nullptr; |
446 | 4 | } |
447 | | |
448 | | // Check for consistency with the previous declaration, if there is one. |
449 | 591 | if (PIDecl) { |
450 | | // A readonly property declared in the primary class can be refined |
451 | | // by adding a readwrite property within an extension. |
452 | | // Anything else is an error. |
453 | 133 | if (!(PIDecl->isReadOnly() && isReadWrite129 )) { |
454 | | // Tailor the diagnostics for the common case where a readwrite |
455 | | // property is declared both in the @interface and the continuation. |
456 | | // This is a common error where the user often intended the original |
457 | | // declaration to be readonly. |
458 | 4 | unsigned diag = |
459 | 4 | (Attributes & ObjCPropertyAttribute::kind_readwrite) && |
460 | 4 | (PIDecl->getPropertyAttributesAsWritten() & |
461 | 1 | ObjCPropertyAttribute::kind_readwrite) |
462 | 4 | ? diag::err_use_continuation_class_redeclaration_readwrite1 |
463 | 4 | : diag::err_use_continuation_class3 ; |
464 | 4 | Diag(AtLoc, diag) |
465 | 4 | << CCPrimary->getDeclName(); |
466 | 4 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
467 | 4 | return nullptr; |
468 | 4 | } |
469 | | |
470 | | // Check for consistency of getters. |
471 | 129 | if (PIDecl->getGetterName() != GetterSel) { |
472 | | // If the getter was written explicitly, complain. |
473 | 2 | if (AttributesAsWritten & ObjCPropertyAttribute::kind_getter) { |
474 | 1 | Diag(AtLoc, diag::warn_property_redecl_getter_mismatch) |
475 | 1 | << PIDecl->getGetterName() << GetterSel; |
476 | 1 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
477 | 1 | } |
478 | | |
479 | | // Always adopt the getter from the original declaration. |
480 | 2 | GetterSel = PIDecl->getGetterName(); |
481 | 2 | Attributes |= ObjCPropertyAttribute::kind_getter; |
482 | 2 | } |
483 | | |
484 | | // Check consistency of ownership. |
485 | 129 | unsigned ExistingOwnership |
486 | 129 | = getOwnershipRule(PIDecl->getPropertyAttributes()); |
487 | 129 | unsigned NewOwnership = getOwnershipRule(Attributes); |
488 | 129 | if (ExistingOwnership && NewOwnership != ExistingOwnership43 ) { |
489 | | // If the ownership was written explicitly, complain. |
490 | 8 | if (getOwnershipRule(AttributesAsWritten)) { |
491 | 2 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
492 | 2 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
493 | 2 | } |
494 | | |
495 | | // Take the ownership from the original property. |
496 | 8 | Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership; |
497 | 8 | } |
498 | | |
499 | | // If the redeclaration is 'weak' but the original property is not, |
500 | 129 | if ((Attributes & ObjCPropertyAttribute::kind_weak) && |
501 | 129 | !(PIDecl->getPropertyAttributesAsWritten() & |
502 | 10 | ObjCPropertyAttribute::kind_weak) && |
503 | 129 | PIDecl->getType()->getAs<ObjCObjectPointerType>()5 && |
504 | 129 | PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None5 ) { |
505 | 1 | Diag(AtLoc, diag::warn_property_implicitly_mismatched); |
506 | 1 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
507 | 1 | } |
508 | 129 | } |
509 | | |
510 | | // Create a new ObjCPropertyDecl with the DeclContext being |
511 | | // the class extension. |
512 | 587 | ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc, |
513 | 587 | FD, GetterSel, GetterNameLoc, |
514 | 587 | SetterSel, SetterNameLoc, |
515 | 587 | isReadWrite, |
516 | 587 | Attributes, AttributesAsWritten, |
517 | 587 | T, TSI, MethodImplKind, DC); |
518 | | |
519 | | // If there was no declaration of a property with the same name in |
520 | | // the primary class, we're done. |
521 | 587 | if (!PIDecl) { |
522 | 458 | ProcessPropertyDecl(PDecl); |
523 | 458 | return PDecl; |
524 | 458 | } |
525 | | |
526 | 129 | if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) { |
527 | 8 | bool IncompatibleObjC = false; |
528 | 8 | QualType ConvertedType; |
529 | | // Relax the strict type matching for property type in continuation class. |
530 | | // Allow property object type of continuation class to be different as long |
531 | | // as it narrows the object type in its primary class property. Note that |
532 | | // this conversion is safe only because the wider type is for a 'readonly' |
533 | | // property in primary class and 'narrowed' type for a 'readwrite' property |
534 | | // in continuation class. |
535 | 8 | QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType()); |
536 | 8 | QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType()); |
537 | 8 | if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) || |
538 | 8 | !isa<ObjCObjectPointerType>(ClassExtPropertyT)7 || |
539 | 8 | (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT, |
540 | 6 | ConvertedType, IncompatibleObjC)) |
541 | 8 | || IncompatibleObjC6 ) { |
542 | 4 | Diag(AtLoc, |
543 | 4 | diag::err_type_mismatch_continuation_class) << PDecl->getType(); |
544 | 4 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
545 | 4 | return nullptr; |
546 | 4 | } |
547 | 8 | } |
548 | | |
549 | | // Check that atomicity of property in class extension matches the previous |
550 | | // declaration. |
551 | 125 | checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true); |
552 | | |
553 | | // Make sure getter/setter are appropriately synthesized. |
554 | 125 | ProcessPropertyDecl(PDecl); |
555 | 125 | return PDecl; |
556 | 129 | } |
557 | | |
558 | | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
559 | | ObjCContainerDecl *CDecl, |
560 | | SourceLocation AtLoc, |
561 | | SourceLocation LParenLoc, |
562 | | FieldDeclarator &FD, |
563 | | Selector GetterSel, |
564 | | SourceLocation GetterNameLoc, |
565 | | Selector SetterSel, |
566 | | SourceLocation SetterNameLoc, |
567 | | const bool isReadWrite, |
568 | | const unsigned Attributes, |
569 | | const unsigned AttributesAsWritten, |
570 | | QualType T, |
571 | | TypeSourceInfo *TInfo, |
572 | | tok::ObjCKeywordKind MethodImplKind, |
573 | 351k | DeclContext *lexicalDC){ |
574 | 351k | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
575 | | |
576 | | // Property defaults to 'assign' if it is readwrite, unless this is ARC |
577 | | // and the type is retainable. |
578 | 351k | bool isAssign; |
579 | 351k | if (Attributes & (ObjCPropertyAttribute::kind_assign | |
580 | 351k | ObjCPropertyAttribute::kind_unsafe_unretained)) { |
581 | 6.08k | isAssign = true; |
582 | 345k | } else if (getOwnershipRule(Attributes) || !isReadWrite117k ) { |
583 | 295k | isAssign = false; |
584 | 295k | } else { |
585 | 50.4k | isAssign = (!getLangOpts().ObjCAutoRefCount || |
586 | 50.4k | !T->isObjCRetainableType()2.31k ); |
587 | 50.4k | } |
588 | | |
589 | | // Issue a warning if property is 'assign' as default and its |
590 | | // object, which is gc'able conforms to NSCopying protocol |
591 | 351k | if (getLangOpts().getGC() != LangOptions::NonGC && isAssign55 && |
592 | 351k | !(Attributes & ObjCPropertyAttribute::kind_assign)46 ) { |
593 | 27 | if (const ObjCObjectPointerType *ObjPtrTy = |
594 | 27 | T->getAs<ObjCObjectPointerType>()) { |
595 | 9 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
596 | 9 | if (IDecl) |
597 | 5 | if (ObjCProtocolDecl* PNSCopying = |
598 | 5 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
599 | 2 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
600 | 2 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
601 | 9 | } |
602 | 27 | } |
603 | | |
604 | 351k | if (T->isObjCObjectType()) { |
605 | 2 | SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc(); |
606 | 2 | StarLoc = getLocForEndOfToken(StarLoc); |
607 | 2 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object) |
608 | 2 | << FixItHint::CreateInsertion(StarLoc, "*"); |
609 | 2 | T = Context.getObjCObjectPointerType(T); |
610 | 2 | SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc(); |
611 | 2 | TInfo = Context.getTrivialTypeSourceInfo(T, TLoc); |
612 | 2 | } |
613 | | |
614 | 351k | DeclContext *DC = CDecl; |
615 | 351k | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
616 | 351k | FD.D.getIdentifierLoc(), |
617 | 351k | PropertyId, AtLoc, |
618 | 351k | LParenLoc, T, TInfo); |
619 | | |
620 | 351k | bool isClassProperty = |
621 | 351k | (AttributesAsWritten & ObjCPropertyAttribute::kind_class) || |
622 | 351k | (Attributes & ObjCPropertyAttribute::kind_class)286k ; |
623 | | // Class property and instance property can have the same name. |
624 | 351k | if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl( |
625 | 351k | DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) { |
626 | 2 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
627 | 2 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
628 | 2 | PDecl->setInvalidDecl(); |
629 | 2 | } |
630 | 351k | else { |
631 | 351k | DC->addDecl(PDecl); |
632 | 351k | if (lexicalDC) |
633 | 587 | PDecl->setLexicalDeclContext(lexicalDC); |
634 | 351k | } |
635 | | |
636 | 351k | if (T->isArrayType() || T->isFunctionType()351k ) { |
637 | 5 | Diag(AtLoc, diag::err_property_type) << T; |
638 | 5 | PDecl->setInvalidDecl(); |
639 | 5 | } |
640 | | |
641 | 351k | ProcessDeclAttributes(S, PDecl, FD.D); |
642 | | |
643 | | // Regardless of setter/getter attribute, we save the default getter/setter |
644 | | // selector names in anticipation of declaration of setter/getter methods. |
645 | 351k | PDecl->setGetterName(GetterSel, GetterNameLoc); |
646 | 351k | PDecl->setSetterName(SetterSel, SetterNameLoc); |
647 | 351k | PDecl->setPropertyAttributesAsWritten( |
648 | 351k | makePropertyAttributesAsWritten(AttributesAsWritten)); |
649 | | |
650 | 351k | if (Attributes & ObjCPropertyAttribute::kind_readonly) |
651 | 234k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readonly); |
652 | | |
653 | 351k | if (Attributes & ObjCPropertyAttribute::kind_getter) |
654 | 16.9k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_getter); |
655 | | |
656 | 351k | if (Attributes & ObjCPropertyAttribute::kind_setter) |
657 | 89 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_setter); |
658 | | |
659 | 351k | if (isReadWrite) |
660 | 117k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readwrite); |
661 | | |
662 | 351k | if (Attributes & ObjCPropertyAttribute::kind_retain) |
663 | 20.0k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_retain); |
664 | | |
665 | 351k | if (Attributes & ObjCPropertyAttribute::kind_strong) |
666 | 12.1k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
667 | | |
668 | 351k | if (Attributes & ObjCPropertyAttribute::kind_weak) |
669 | 1.07k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_weak); |
670 | | |
671 | 351k | if (Attributes & ObjCPropertyAttribute::kind_copy) |
672 | 195k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_copy); |
673 | | |
674 | 351k | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
675 | 48 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained); |
676 | | |
677 | 351k | if (isAssign) |
678 | 56.4k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign); |
679 | | |
680 | | // In the semantic attributes, one of nonatomic or atomic is always set. |
681 | 351k | if (Attributes & ObjCPropertyAttribute::kind_nonatomic) |
682 | 4.83k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nonatomic); |
683 | 346k | else |
684 | 346k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_atomic); |
685 | | |
686 | | // 'unsafe_unretained' is alias for 'assign'. |
687 | 351k | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
688 | 48 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign); |
689 | 351k | if (isAssign) |
690 | 56.4k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained); |
691 | | |
692 | 351k | if (MethodImplKind == tok::objc_required) |
693 | 1.24k | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
694 | 350k | else if (MethodImplKind == tok::objc_optional) |
695 | 1.06k | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
696 | | |
697 | 351k | if (Attributes & ObjCPropertyAttribute::kind_nullability) |
698 | 109k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nullability); |
699 | | |
700 | 351k | if (Attributes & ObjCPropertyAttribute::kind_null_resettable) |
701 | 13.7k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_null_resettable); |
702 | | |
703 | 351k | if (Attributes & ObjCPropertyAttribute::kind_class) |
704 | 65.2k | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_class); |
705 | | |
706 | 351k | if ((Attributes & ObjCPropertyAttribute::kind_direct) || |
707 | 351k | CDecl->hasAttr<ObjCDirectMembersAttr>()351k ) { |
708 | 58 | if (isa<ObjCProtocolDecl>(CDecl)) { |
709 | 1 | Diag(PDecl->getLocation(), diag::err_objc_direct_on_protocol) << true; |
710 | 57 | } else if (getLangOpts().ObjCRuntime.allowsDirectDispatch()) { |
711 | 57 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_direct); |
712 | 57 | } else { |
713 | 0 | Diag(PDecl->getLocation(), diag::warn_objc_direct_property_ignored) |
714 | 0 | << PDecl->getDeclName(); |
715 | 0 | } |
716 | 58 | } |
717 | | |
718 | 351k | return PDecl; |
719 | 351k | } |
720 | | |
721 | | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
722 | | ObjCPropertyDecl *property, |
723 | 706 | ObjCIvarDecl *ivar) { |
724 | 706 | if (property->isInvalidDecl() || ivar->isInvalidDecl()638 ) return71 ; |
725 | | |
726 | 635 | QualType ivarType = ivar->getType(); |
727 | 635 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
728 | | |
729 | | // The lifetime implied by the property's attributes. |
730 | 635 | Qualifiers::ObjCLifetime propertyLifetime = |
731 | 635 | getImpliedARCOwnership(property->getPropertyAttributes(), |
732 | 635 | property->getType()); |
733 | | |
734 | | // We're fine if they match. |
735 | 635 | if (propertyLifetime == ivarLifetime) return531 ; |
736 | | |
737 | | // None isn't a valid lifetime for an object ivar in ARC, and |
738 | | // __autoreleasing is never valid; don't diagnose twice. |
739 | 104 | if ((ivarLifetime == Qualifiers::OCL_None && |
740 | 104 | S.getLangOpts().ObjCAutoRefCount58 ) || |
741 | 104 | ivarLifetime == Qualifiers::OCL_Autoreleasing48 ) |
742 | 60 | return; |
743 | | |
744 | | // If the ivar is private, and it's implicitly __unsafe_unretained |
745 | | // because of its type, then pretend it was actually implicitly |
746 | | // __strong. This is only sound because we're processing the |
747 | | // property implementation before parsing any method bodies. |
748 | 44 | if (ivarLifetime == Qualifiers::OCL_ExplicitNone && |
749 | 44 | propertyLifetime == Qualifiers::OCL_Strong6 && |
750 | 44 | ivar->getAccessControl() == ObjCIvarDecl::Private5 ) { |
751 | 2 | SplitQualType split = ivarType.split(); |
752 | 2 | if (split.Quals.hasObjCLifetime()) { |
753 | 2 | assert(ivarType->isObjCARCImplicitlyUnretainedType()); |
754 | 0 | split.Quals.setObjCLifetime(Qualifiers::OCL_Strong); |
755 | 2 | ivarType = S.Context.getQualifiedType(split); |
756 | 2 | ivar->setType(ivarType); |
757 | 2 | return; |
758 | 2 | } |
759 | 2 | } |
760 | | |
761 | 42 | switch (propertyLifetime) { |
762 | 9 | case Qualifiers::OCL_Strong: |
763 | 9 | S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership) |
764 | 9 | << property->getDeclName() |
765 | 9 | << ivar->getDeclName() |
766 | 9 | << ivarLifetime; |
767 | 9 | break; |
768 | | |
769 | 6 | case Qualifiers::OCL_Weak: |
770 | 6 | S.Diag(ivar->getLocation(), diag::err_weak_property) |
771 | 6 | << property->getDeclName() |
772 | 6 | << ivar->getDeclName(); |
773 | 6 | break; |
774 | | |
775 | 27 | case Qualifiers::OCL_ExplicitNone: |
776 | 27 | S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership) |
777 | 27 | << property->getDeclName() << ivar->getDeclName() |
778 | 27 | << ((property->getPropertyAttributesAsWritten() & |
779 | 27 | ObjCPropertyAttribute::kind_assign) != 0); |
780 | 27 | break; |
781 | | |
782 | 0 | case Qualifiers::OCL_Autoreleasing: |
783 | 0 | llvm_unreachable("properties cannot be autoreleasing"); |
784 | |
|
785 | 0 | case Qualifiers::OCL_None: |
786 | | // Any other property should be ignored. |
787 | 0 | return; |
788 | 42 | } |
789 | | |
790 | 42 | S.Diag(property->getLocation(), diag::note_property_declare); |
791 | 42 | if (propertyImplLoc.isValid()) |
792 | 41 | S.Diag(propertyImplLoc, diag::note_property_synthesize); |
793 | 42 | } |
794 | | |
795 | | /// setImpliedPropertyAttributeForReadOnlyProperty - |
796 | | /// This routine evaludates life-time attributes for a 'readonly' |
797 | | /// property with no known lifetime of its own, using backing |
798 | | /// 'ivar's attribute, if any. If no backing 'ivar', property's |
799 | | /// life-time is assumed 'strong'. |
800 | | static void setImpliedPropertyAttributeForReadOnlyProperty( |
801 | 112 | ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { |
802 | 112 | Qualifiers::ObjCLifetime propertyLifetime = |
803 | 112 | getImpliedARCOwnership(property->getPropertyAttributes(), |
804 | 112 | property->getType()); |
805 | 112 | if (propertyLifetime != Qualifiers::OCL_None) |
806 | 54 | return; |
807 | | |
808 | 58 | if (!ivar) { |
809 | | // if no backing ivar, make property 'strong'. |
810 | 43 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
811 | 43 | return; |
812 | 43 | } |
813 | | // property assumes owenership of backing ivar. |
814 | 15 | QualType ivarType = ivar->getType(); |
815 | 15 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
816 | 15 | if (ivarLifetime == Qualifiers::OCL_Strong) |
817 | 10 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
818 | 5 | else if (ivarLifetime == Qualifiers::OCL_Weak) |
819 | 5 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_weak); |
820 | 15 | } |
821 | | |
822 | | static bool isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2, |
823 | 27 | ObjCPropertyAttribute::Kind Kind) { |
824 | 27 | return (Attr1 & Kind) != (Attr2 & Kind); |
825 | 27 | } |
826 | | |
827 | | static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2, |
828 | 11 | unsigned Kinds) { |
829 | 11 | return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0); |
830 | 11 | } |
831 | | |
832 | | /// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate |
833 | | /// property declaration that should be synthesised in all of the inherited |
834 | | /// protocols. It also diagnoses properties declared in inherited protocols with |
835 | | /// mismatched types or attributes, since any of them can be candidate for |
836 | | /// synthesis. |
837 | | static ObjCPropertyDecl * |
838 | | SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc, |
839 | | ObjCInterfaceDecl *ClassDecl, |
840 | 90 | ObjCPropertyDecl *Property) { |
841 | 90 | assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) && |
842 | 90 | "Expected a property from a protocol"); |
843 | 0 | ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet; |
844 | 90 | ObjCInterfaceDecl::PropertyDeclOrder Properties; |
845 | 121 | for (const auto *PI : ClassDecl->all_referenced_protocols()) { |
846 | 121 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
847 | 121 | PDecl->collectInheritedProtocolProperties(Property, ProtocolSet, |
848 | 121 | Properties); |
849 | 121 | } |
850 | 90 | if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) { |
851 | 81 | while (SDecl) { |
852 | 41 | for (const auto *PI : SDecl->all_referenced_protocols()) { |
853 | 6 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
854 | 6 | PDecl->collectInheritedProtocolProperties(Property, ProtocolSet, |
855 | 6 | Properties); |
856 | 6 | } |
857 | 41 | SDecl = SDecl->getSuperClass(); |
858 | 41 | } |
859 | 40 | } |
860 | | |
861 | 90 | if (Properties.empty()) |
862 | 74 | return Property; |
863 | | |
864 | 16 | ObjCPropertyDecl *OriginalProperty = Property; |
865 | 16 | size_t SelectedIndex = 0; |
866 | 20 | for (const auto &Prop : llvm::enumerate(Properties)) { |
867 | | // Select the 'readwrite' property if such property exists. |
868 | 20 | if (Property->isReadOnly() && !Prop.value()->isReadOnly()10 ) { |
869 | 3 | Property = Prop.value(); |
870 | 3 | SelectedIndex = Prop.index(); |
871 | 3 | } |
872 | 20 | } |
873 | 16 | if (Property != OriginalProperty) { |
874 | | // Check that the old property is compatible with the new one. |
875 | 3 | Properties[SelectedIndex] = OriginalProperty; |
876 | 3 | } |
877 | | |
878 | 16 | QualType RHSType = S.Context.getCanonicalType(Property->getType()); |
879 | 16 | unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten(); |
880 | 16 | enum MismatchKind { |
881 | 16 | IncompatibleType = 0, |
882 | 16 | HasNoExpectedAttribute, |
883 | 16 | HasUnexpectedAttribute, |
884 | 16 | DifferentGetter, |
885 | 16 | DifferentSetter |
886 | 16 | }; |
887 | | // Represents a property from another protocol that conflicts with the |
888 | | // selected declaration. |
889 | 16 | struct MismatchingProperty { |
890 | 16 | const ObjCPropertyDecl *Prop; |
891 | 16 | MismatchKind Kind; |
892 | 16 | StringRef AttributeName; |
893 | 16 | }; |
894 | 16 | SmallVector<MismatchingProperty, 4> Mismatches; |
895 | 20 | for (ObjCPropertyDecl *Prop : Properties) { |
896 | | // Verify the property attributes. |
897 | 20 | unsigned Attr = Prop->getPropertyAttributesAsWritten(); |
898 | 20 | if (Attr != OriginalAttributes) { |
899 | 18 | auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) { |
900 | 5 | MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute2 |
901 | 5 | : HasUnexpectedAttribute3 ; |
902 | 5 | Mismatches.push_back({Prop, Kind, AttributeName}); |
903 | 5 | }; |
904 | | // The ownership might be incompatible unless the property has no explicit |
905 | | // ownership. |
906 | 18 | bool HasOwnership = |
907 | 18 | (Attr & (ObjCPropertyAttribute::kind_retain | |
908 | 18 | ObjCPropertyAttribute::kind_strong | |
909 | 18 | ObjCPropertyAttribute::kind_copy | |
910 | 18 | ObjCPropertyAttribute::kind_assign | |
911 | 18 | ObjCPropertyAttribute::kind_unsafe_unretained | |
912 | 18 | ObjCPropertyAttribute::kind_weak)) != 0; |
913 | 18 | if (HasOwnership && |
914 | 18 | isIncompatiblePropertyAttribute(OriginalAttributes, Attr, |
915 | 13 | ObjCPropertyAttribute::kind_copy)) { |
916 | 2 | Diag(OriginalAttributes & ObjCPropertyAttribute::kind_copy, "copy"); |
917 | 2 | continue; |
918 | 2 | } |
919 | 16 | if (HasOwnership && areIncompatiblePropertyAttributes( |
920 | 11 | OriginalAttributes, Attr, |
921 | 11 | ObjCPropertyAttribute::kind_retain | |
922 | 11 | ObjCPropertyAttribute::kind_strong)) { |
923 | 2 | Diag(OriginalAttributes & (ObjCPropertyAttribute::kind_retain | |
924 | 2 | ObjCPropertyAttribute::kind_strong), |
925 | 2 | "retain (or strong)"); |
926 | 2 | continue; |
927 | 2 | } |
928 | 14 | if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr, |
929 | 14 | ObjCPropertyAttribute::kind_atomic)) { |
930 | 1 | Diag(OriginalAttributes & ObjCPropertyAttribute::kind_atomic, "atomic"); |
931 | 1 | continue; |
932 | 1 | } |
933 | 14 | } |
934 | 15 | if (Property->getGetterName() != Prop->getGetterName()) { |
935 | 1 | Mismatches.push_back({Prop, DifferentGetter, ""}); |
936 | 1 | continue; |
937 | 1 | } |
938 | 14 | if (!Property->isReadOnly() && !Prop->isReadOnly()13 && |
939 | 14 | Property->getSetterName() != Prop->getSetterName()6 ) { |
940 | 1 | Mismatches.push_back({Prop, DifferentSetter, ""}); |
941 | 1 | continue; |
942 | 1 | } |
943 | 13 | QualType LHSType = S.Context.getCanonicalType(Prop->getType()); |
944 | 13 | if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
945 | 6 | bool IncompatibleObjC = false; |
946 | 6 | QualType ConvertedType; |
947 | 6 | if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC) |
948 | 6 | || IncompatibleObjC0 ) { |
949 | 6 | Mismatches.push_back({Prop, IncompatibleType, ""}); |
950 | 6 | continue; |
951 | 6 | } |
952 | 6 | } |
953 | 13 | } |
954 | | |
955 | 16 | if (Mismatches.empty()) |
956 | 5 | return Property; |
957 | | |
958 | | // Diagnose incompability. |
959 | 11 | { |
960 | 11 | bool HasIncompatibleAttributes = false; |
961 | 11 | for (const auto &Note : Mismatches) |
962 | 13 | HasIncompatibleAttributes = |
963 | 13 | Note.Kind != IncompatibleType ? true7 : HasIncompatibleAttributes6 ; |
964 | | // Promote the warning to an error if there are incompatible attributes or |
965 | | // incompatible types together with readwrite/readonly incompatibility. |
966 | 11 | auto Diag = S.Diag(Property->getLocation(), |
967 | 11 | Property != OriginalProperty || HasIncompatibleAttributes9 |
968 | 11 | ? diag::err_protocol_property_mismatch8 |
969 | 11 | : diag::warn_protocol_property_mismatch3 ); |
970 | 11 | Diag << Mismatches[0].Kind; |
971 | 11 | switch (Mismatches[0].Kind) { |
972 | 5 | case IncompatibleType: |
973 | 5 | Diag << Property->getType(); |
974 | 5 | break; |
975 | 2 | case HasNoExpectedAttribute: |
976 | 4 | case HasUnexpectedAttribute: |
977 | 4 | Diag << Mismatches[0].AttributeName; |
978 | 4 | break; |
979 | 1 | case DifferentGetter: |
980 | 1 | Diag << Property->getGetterName(); |
981 | 1 | break; |
982 | 1 | case DifferentSetter: |
983 | 1 | Diag << Property->getSetterName(); |
984 | 1 | break; |
985 | 11 | } |
986 | 11 | } |
987 | 13 | for (const auto &Note : Mismatches)11 { |
988 | 13 | auto Diag = |
989 | 13 | S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare) |
990 | 13 | << Note.Kind; |
991 | 13 | switch (Note.Kind) { |
992 | 6 | case IncompatibleType: |
993 | 6 | Diag << Note.Prop->getType(); |
994 | 6 | break; |
995 | 2 | case HasNoExpectedAttribute: |
996 | 5 | case HasUnexpectedAttribute: |
997 | 5 | Diag << Note.AttributeName; |
998 | 5 | break; |
999 | 1 | case DifferentGetter: |
1000 | 1 | Diag << Note.Prop->getGetterName(); |
1001 | 1 | break; |
1002 | 1 | case DifferentSetter: |
1003 | 1 | Diag << Note.Prop->getSetterName(); |
1004 | 1 | break; |
1005 | 13 | } |
1006 | 13 | } |
1007 | 11 | if (AtLoc.isValid()) |
1008 | 11 | S.Diag(AtLoc, diag::note_property_synthesize); |
1009 | | |
1010 | 11 | return Property; |
1011 | 11 | } |
1012 | | |
1013 | | /// Determine whether any storage attributes were written on the property. |
1014 | | static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop, |
1015 | 382 | ObjCPropertyQueryKind QueryKind) { |
1016 | 382 | if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true320 ; |
1017 | | |
1018 | | // If this is a readwrite property in a class extension that refines |
1019 | | // a readonly property in the original class definition, check it as |
1020 | | // well. |
1021 | | |
1022 | | // If it's a readonly property, we're not interested. |
1023 | 62 | if (Prop->isReadOnly()) return false43 ; |
1024 | | |
1025 | | // Is it declared in an extension? |
1026 | 19 | auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext()); |
1027 | 19 | if (!Category || !Category->IsClassExtension()8 ) return false11 ; |
1028 | | |
1029 | | // Find the corresponding property in the primary class definition. |
1030 | 8 | auto OrigClass = Category->getClassInterface(); |
1031 | 8 | for (auto Found : OrigClass->lookup(Prop->getDeclName())) { |
1032 | 7 | if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found)) |
1033 | 7 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
1034 | 7 | } |
1035 | | |
1036 | | // Look through all of the protocols. |
1037 | 1 | for (const auto *Proto : OrigClass->all_referenced_protocols()) { |
1038 | 1 | if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration( |
1039 | 1 | Prop->getIdentifier(), QueryKind)) |
1040 | 1 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
1041 | 1 | } |
1042 | | |
1043 | 0 | return false; |
1044 | 1 | } |
1045 | | |
1046 | | /// Create a synthesized property accessor stub inside the \@implementation. |
1047 | | static ObjCMethodDecl * |
1048 | | RedeclarePropertyAccessor(ASTContext &Context, ObjCImplementationDecl *Impl, |
1049 | | ObjCMethodDecl *AccessorDecl, SourceLocation AtLoc, |
1050 | 5.12k | SourceLocation PropertyLoc) { |
1051 | 5.12k | ObjCMethodDecl *Decl = AccessorDecl; |
1052 | 5.12k | ObjCMethodDecl *ImplDecl = ObjCMethodDecl::Create( |
1053 | 5.12k | Context, AtLoc.isValid() ? AtLoc2.89k : Decl->getBeginLoc()2.23k , |
1054 | 5.12k | PropertyLoc.isValid() ? PropertyLoc2.89k : Decl->getEndLoc()2.23k , |
1055 | 5.12k | Decl->getSelector(), Decl->getReturnType(), |
1056 | 5.12k | Decl->getReturnTypeSourceInfo(), Impl, Decl->isInstanceMethod(), |
1057 | 5.12k | Decl->isVariadic(), Decl->isPropertyAccessor(), |
1058 | 5.12k | /* isSynthesized*/ true, Decl->isImplicit(), Decl->isDefined(), |
1059 | 5.12k | Decl->getImplementationControl(), Decl->hasRelatedResultType()); |
1060 | 5.12k | ImplDecl->getMethodFamily(); |
1061 | 5.12k | if (Decl->hasAttrs()) |
1062 | 110 | ImplDecl->setAttrs(Decl->getAttrs()); |
1063 | 5.12k | ImplDecl->setSelfDecl(Decl->getSelfDecl()); |
1064 | 5.12k | ImplDecl->setCmdDecl(Decl->getCmdDecl()); |
1065 | 5.12k | SmallVector<SourceLocation, 1> SelLocs; |
1066 | 5.12k | Decl->getSelectorLocs(SelLocs); |
1067 | 5.12k | ImplDecl->setMethodParams(Context, Decl->parameters(), SelLocs); |
1068 | 5.12k | ImplDecl->setLexicalDeclContext(Impl); |
1069 | 5.12k | ImplDecl->setDefined(false); |
1070 | 5.12k | return ImplDecl; |
1071 | 5.12k | } |
1072 | | |
1073 | | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
1074 | | /// builds the AST node for a property implementation declaration; declared |
1075 | | /// as \@synthesize or \@dynamic. |
1076 | | /// |
1077 | | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
1078 | | SourceLocation AtLoc, |
1079 | | SourceLocation PropertyLoc, |
1080 | | bool Synthesize, |
1081 | | IdentifierInfo *PropertyId, |
1082 | | IdentifierInfo *PropertyIvar, |
1083 | | SourceLocation PropertyIvarLoc, |
1084 | 3.01k | ObjCPropertyQueryKind QueryKind) { |
1085 | 3.01k | ObjCContainerDecl *ClassImpDecl = |
1086 | 3.01k | dyn_cast<ObjCContainerDecl>(CurContext); |
1087 | | // Make sure we have a context for the property implementation declaration. |
1088 | 3.01k | if (!ClassImpDecl) { |
1089 | 1 | Diag(AtLoc, diag::err_missing_property_context); |
1090 | 1 | return nullptr; |
1091 | 1 | } |
1092 | 3.01k | if (PropertyIvarLoc.isInvalid()) |
1093 | 1.23k | PropertyIvarLoc = PropertyLoc; |
1094 | 3.01k | SourceLocation PropertyDiagLoc = PropertyLoc; |
1095 | 3.01k | if (PropertyDiagLoc.isInvalid()) |
1096 | 1.24k | PropertyDiagLoc = ClassImpDecl->getBeginLoc(); |
1097 | 3.01k | ObjCPropertyDecl *property = nullptr; |
1098 | 3.01k | ObjCInterfaceDecl *IDecl = nullptr; |
1099 | | // Find the class or category class where this property must have |
1100 | | // a declaration. |
1101 | 3.01k | ObjCImplementationDecl *IC = nullptr; |
1102 | 3.01k | ObjCCategoryImplDecl *CatImplClass = nullptr; |
1103 | 3.01k | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
1104 | 3.00k | IDecl = IC->getClassInterface(); |
1105 | | // We always synthesize an interface for an implementation |
1106 | | // without an interface decl. So, IDecl is always non-zero. |
1107 | 3.00k | assert(IDecl && |
1108 | 3.00k | "ActOnPropertyImplDecl - @implementation without @interface"); |
1109 | | |
1110 | | // Look for this property declaration in the @implementation's @interface |
1111 | 0 | property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind); |
1112 | 3.00k | if (!property) { |
1113 | 2 | Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName(); |
1114 | 2 | return nullptr; |
1115 | 2 | } |
1116 | 3.00k | if (property->isClassProperty() && Synthesize8 ) { |
1117 | 1 | Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId; |
1118 | 1 | return nullptr; |
1119 | 1 | } |
1120 | 3.00k | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
1121 | 3.00k | if ((PIkind & (ObjCPropertyAttribute::kind_atomic | |
1122 | 3.00k | ObjCPropertyAttribute::kind_nonatomic)) == 0) { |
1123 | 2.42k | if (AtLoc.isValid()) |
1124 | 1.52k | Diag(AtLoc, diag::warn_implicit_atomic_property); |
1125 | 904 | else |
1126 | 904 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
1127 | 2.42k | Diag(property->getLocation(), diag::note_property_declare); |
1128 | 2.42k | } |
1129 | | |
1130 | 3.00k | if (const ObjCCategoryDecl *CD = |
1131 | 3.00k | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
1132 | 245 | if (!CD->IsClassExtension()) { |
1133 | 2 | Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName(); |
1134 | 2 | Diag(property->getLocation(), diag::note_property_declare); |
1135 | 2 | return nullptr; |
1136 | 2 | } |
1137 | 245 | } |
1138 | 3.00k | if (Synthesize && (PIkind & ObjCPropertyAttribute::kind_readonly)2.78k && |
1139 | 3.00k | property->hasAttr<IBOutletAttr>()362 && !AtLoc.isValid()8 ) { |
1140 | 8 | bool ReadWriteProperty = false; |
1141 | | // Search into the class extensions and see if 'readonly property is |
1142 | | // redeclared 'readwrite', then no warning is to be issued. |
1143 | 8 | for (auto *Ext : IDecl->known_extensions()) { |
1144 | 2 | DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); |
1145 | 2 | if (auto *ExtProp = R.find_first<ObjCPropertyDecl>()) { |
1146 | 0 | PIkind = ExtProp->getPropertyAttributesAsWritten(); |
1147 | 0 | if (PIkind & ObjCPropertyAttribute::kind_readwrite) { |
1148 | 0 | ReadWriteProperty = true; |
1149 | 0 | break; |
1150 | 0 | } |
1151 | 0 | } |
1152 | 2 | } |
1153 | | |
1154 | 8 | if (!ReadWriteProperty) { |
1155 | 8 | Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property) |
1156 | 8 | << property; |
1157 | 8 | SourceLocation readonlyLoc; |
1158 | 8 | if (LocPropertyAttribute(Context, "readonly", |
1159 | 8 | property->getLParenLoc(), readonlyLoc)) { |
1160 | 6 | SourceLocation endLoc = |
1161 | 6 | readonlyLoc.getLocWithOffset(strlen("readonly")-1); |
1162 | 6 | SourceRange ReadonlySourceRange(readonlyLoc, endLoc); |
1163 | 6 | Diag(property->getLocation(), |
1164 | 6 | diag::note_auto_readonly_iboutlet_fixup_suggest) << |
1165 | 6 | FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); |
1166 | 6 | } |
1167 | 8 | } |
1168 | 8 | } |
1169 | 3.00k | if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext())2.78k ) |
1170 | 90 | property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl, |
1171 | 90 | property); |
1172 | | |
1173 | 3.00k | } else if (9 (CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))9 ) { |
1174 | 9 | if (Synthesize) { |
1175 | 2 | Diag(AtLoc, diag::err_synthesize_category_decl); |
1176 | 2 | return nullptr; |
1177 | 2 | } |
1178 | 7 | IDecl = CatImplClass->getClassInterface(); |
1179 | 7 | if (!IDecl) { |
1180 | 1 | Diag(AtLoc, diag::err_missing_property_interface); |
1181 | 1 | return nullptr; |
1182 | 1 | } |
1183 | 6 | ObjCCategoryDecl *Category = |
1184 | 6 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
1185 | | |
1186 | | // If category for this implementation not found, it is an error which |
1187 | | // has already been reported eralier. |
1188 | 6 | if (!Category) |
1189 | 0 | return nullptr; |
1190 | | // Look for this property declaration in @implementation's category |
1191 | 6 | property = Category->FindPropertyDeclaration(PropertyId, QueryKind); |
1192 | 6 | if (!property) { |
1193 | 1 | Diag(PropertyLoc, diag::err_bad_category_property_decl) |
1194 | 1 | << Category->getDeclName(); |
1195 | 1 | return nullptr; |
1196 | 1 | } |
1197 | 6 | } else { |
1198 | 0 | Diag(AtLoc, diag::err_bad_property_context); |
1199 | 0 | return nullptr; |
1200 | 0 | } |
1201 | 3.00k | ObjCIvarDecl *Ivar = nullptr; |
1202 | 3.00k | bool CompleteTypeErr = false; |
1203 | 3.00k | bool compat = true; |
1204 | | // Check that we have a valid, previously declared ivar for @synthesize |
1205 | 3.00k | if (Synthesize) { |
1206 | | // @synthesize |
1207 | 2.78k | if (!PropertyIvar) |
1208 | 1.00k | PropertyIvar = PropertyId; |
1209 | | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
1210 | 2.78k | ObjCInterfaceDecl *ClassDeclared; |
1211 | 2.78k | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
1212 | 2.78k | QualType PropType = property->getType(); |
1213 | 2.78k | QualType PropertyIvarType = PropType.getNonReferenceType(); |
1214 | | |
1215 | 2.78k | if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, |
1216 | 2.78k | diag::err_incomplete_synthesized_property, |
1217 | 2.78k | property->getDeclName())) { |
1218 | 4 | Diag(property->getLocation(), diag::note_property_declare); |
1219 | 4 | CompleteTypeErr = true; |
1220 | 4 | } |
1221 | | |
1222 | 2.78k | if (getLangOpts().ObjCAutoRefCount && |
1223 | 2.78k | (property->getPropertyAttributesAsWritten() & |
1224 | 691 | ObjCPropertyAttribute::kind_readonly) && |
1225 | 2.78k | PropertyIvarType->isObjCRetainableType()126 ) { |
1226 | 112 | setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); |
1227 | 112 | } |
1228 | | |
1229 | 2.78k | ObjCPropertyAttribute::Kind kind = property->getPropertyAttributes(); |
1230 | | |
1231 | 2.78k | bool isARCWeak = false; |
1232 | 2.78k | if (kind & ObjCPropertyAttribute::kind_weak) { |
1233 | | // Add GC __weak to the ivar type if the property is weak. |
1234 | 112 | if (getLangOpts().getGC() != LangOptions::NonGC) { |
1235 | 3 | assert(!getLangOpts().ObjCAutoRefCount); |
1236 | 3 | if (PropertyIvarType.isObjCGCStrong()) { |
1237 | 0 | Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); |
1238 | 0 | Diag(property->getLocation(), diag::note_property_declare); |
1239 | 3 | } else { |
1240 | 3 | PropertyIvarType = |
1241 | 3 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
1242 | 3 | } |
1243 | | |
1244 | | // Otherwise, check whether ARC __weak is enabled and works with |
1245 | | // the property type. |
1246 | 109 | } else { |
1247 | 109 | if (!getLangOpts().ObjCWeak) { |
1248 | | // Only complain here when synthesizing an ivar. |
1249 | 11 | if (!Ivar) { |
1250 | 4 | Diag(PropertyDiagLoc, |
1251 | 4 | getLangOpts().ObjCWeakRuntime |
1252 | 4 | ? diag::err_synthesizing_arc_weak_property_disabled2 |
1253 | 4 | : diag::err_synthesizing_arc_weak_property_no_runtime2 ); |
1254 | 4 | Diag(property->getLocation(), diag::note_property_declare); |
1255 | 4 | } |
1256 | 11 | CompleteTypeErr = true; // suppress later diagnostics about the ivar |
1257 | 98 | } else { |
1258 | 98 | isARCWeak = true; |
1259 | 98 | if (const ObjCObjectPointerType *ObjT = |
1260 | 98 | PropertyIvarType->getAs<ObjCObjectPointerType>()) { |
1261 | 98 | const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); |
1262 | 98 | if (ObjI && ObjI->isArcWeakrefUnavailable()67 ) { |
1263 | 6 | Diag(property->getLocation(), |
1264 | 6 | diag::err_arc_weak_unavailable_property) |
1265 | 6 | << PropertyIvarType; |
1266 | 6 | Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class) |
1267 | 6 | << ClassImpDecl->getName(); |
1268 | 6 | } |
1269 | 98 | } |
1270 | 98 | } |
1271 | 109 | } |
1272 | 112 | } |
1273 | | |
1274 | 2.78k | if (AtLoc.isInvalid()) { |
1275 | | // Check when default synthesizing a property that there is |
1276 | | // an ivar matching property name and issue warning; since this |
1277 | | // is the most common case of not using an ivar used for backing |
1278 | | // property in non-default synthesis case. |
1279 | 1.24k | ObjCInterfaceDecl *ClassDeclared=nullptr; |
1280 | 1.24k | ObjCIvarDecl *originalIvar = |
1281 | 1.24k | IDecl->lookupInstanceVariable(property->getIdentifier(), |
1282 | 1.24k | ClassDeclared); |
1283 | 1.24k | if (originalIvar) { |
1284 | 4 | Diag(PropertyDiagLoc, |
1285 | 4 | diag::warn_autosynthesis_property_ivar_match) |
1286 | 4 | << PropertyId << (Ivar == nullptr) << PropertyIvar |
1287 | 4 | << originalIvar->getIdentifier(); |
1288 | 4 | Diag(property->getLocation(), diag::note_property_declare); |
1289 | 4 | Diag(originalIvar->getLocation(), diag::note_ivar_decl); |
1290 | 4 | } |
1291 | 1.24k | } |
1292 | | |
1293 | 2.78k | if (!Ivar) { |
1294 | | // In ARC, give the ivar a lifetime qualifier based on the |
1295 | | // property attributes. |
1296 | 2.00k | if ((getLangOpts().ObjCAutoRefCount || isARCWeak1.50k ) && |
1297 | 2.00k | !PropertyIvarType.getObjCLifetime()510 && |
1298 | 2.00k | PropertyIvarType->isObjCRetainableType()450 ) { |
1299 | | |
1300 | | // It's an error if we have to do this and the user didn't |
1301 | | // explicitly write an ownership attribute on the property. |
1302 | 382 | if (!hasWrittenStorageAttribute(property, QueryKind) && |
1303 | 382 | !(kind & ObjCPropertyAttribute::kind_strong)60 ) { |
1304 | 0 | Diag(PropertyDiagLoc, |
1305 | 0 | diag::err_arc_objc_property_default_assign_on_object); |
1306 | 0 | Diag(property->getLocation(), diag::note_property_declare); |
1307 | 382 | } else { |
1308 | 382 | Qualifiers::ObjCLifetime lifetime = |
1309 | 382 | getImpliedARCOwnership(kind, PropertyIvarType); |
1310 | 382 | assert(lifetime && "no lifetime for property?"); |
1311 | | |
1312 | 0 | Qualifiers qs; |
1313 | 382 | qs.addObjCLifetime(lifetime); |
1314 | 382 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
1315 | 382 | } |
1316 | 382 | } |
1317 | | |
1318 | 0 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
1319 | 2.00k | PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, |
1320 | 2.00k | PropertyIvarType, /*TInfo=*/nullptr, |
1321 | 2.00k | ObjCIvarDecl::Private, |
1322 | 2.00k | (Expr *)nullptr, true); |
1323 | 2.00k | if (RequireNonAbstractType(PropertyIvarLoc, |
1324 | 2.00k | PropertyIvarType, |
1325 | 2.00k | diag::err_abstract_type_in_decl, |
1326 | 2.00k | AbstractSynthesizedIvarType)) { |
1327 | 1 | Diag(property->getLocation(), diag::note_property_declare); |
1328 | | // An abstract type is as bad as an incomplete type. |
1329 | 1 | CompleteTypeErr = true; |
1330 | 1 | } |
1331 | 2.00k | if (!CompleteTypeErr) { |
1332 | 1.99k | const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>(); |
1333 | 1.99k | if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()44 ) { |
1334 | 2 | Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar) |
1335 | 2 | << PropertyIvarType; |
1336 | 2 | CompleteTypeErr = true; // suppress later diagnostics about the ivar |
1337 | 2 | } |
1338 | 1.99k | } |
1339 | 2.00k | if (CompleteTypeErr) |
1340 | 11 | Ivar->setInvalidDecl(); |
1341 | 2.00k | ClassImpDecl->addDecl(Ivar); |
1342 | 2.00k | IDecl->makeDeclVisibleInContext(Ivar); |
1343 | | |
1344 | 2.00k | if (getLangOpts().ObjCRuntime.isFragile()) |
1345 | 2 | Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl) |
1346 | 2 | << PropertyId; |
1347 | | // Note! I deliberately want it to fall thru so, we have a |
1348 | | // a property implementation and to avoid future warnings. |
1349 | 2.00k | } else if (782 getLangOpts().ObjCRuntime.isNonFragile()782 && |
1350 | 782 | !declaresSameEntity(ClassDeclared, IDecl)750 ) { |
1351 | 31 | Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use) |
1352 | 31 | << property->getDeclName() << Ivar->getDeclName() |
1353 | 31 | << ClassDeclared->getDeclName(); |
1354 | 31 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
1355 | 31 | << Ivar << Ivar->getName(); |
1356 | | // Note! I deliberately want it to fall thru so more errors are caught. |
1357 | 31 | } |
1358 | 0 | property->setPropertyIvarDecl(Ivar); |
1359 | | |
1360 | 2.78k | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
1361 | | |
1362 | | // Check that type of property and its ivar are type compatible. |
1363 | 2.78k | if (!Context.hasSameType(PropertyIvarType, IvarType)) { |
1364 | 197 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
1365 | 197 | && isa<ObjCObjectPointerType>(IvarType)103 ) |
1366 | 103 | compat = |
1367 | 103 | Context.canAssignObjCInterfaces( |
1368 | 103 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
1369 | 103 | IvarType->getAs<ObjCObjectPointerType>()); |
1370 | 94 | else { |
1371 | 94 | compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, |
1372 | 94 | IvarType) |
1373 | 94 | == Compatible); |
1374 | 94 | } |
1375 | 197 | if (!compat) { |
1376 | 2 | Diag(PropertyDiagLoc, diag::err_property_ivar_type) |
1377 | 2 | << property->getDeclName() << PropType |
1378 | 2 | << Ivar->getDeclName() << IvarType; |
1379 | 2 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
1380 | | // Note! I deliberately want it to fall thru so, we have a |
1381 | | // a property implementation and to avoid future warnings. |
1382 | 2 | } |
1383 | 195 | else { |
1384 | | // FIXME! Rules for properties are somewhat different that those |
1385 | | // for assignments. Use a new routine to consolidate all cases; |
1386 | | // specifically for property redeclarations as well as for ivars. |
1387 | 195 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
1388 | 195 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
1389 | 195 | if (lhsType != rhsType && |
1390 | 195 | lhsType->isArithmeticType()19 ) { |
1391 | 1 | Diag(PropertyDiagLoc, diag::err_property_ivar_type) |
1392 | 1 | << property->getDeclName() << PropType |
1393 | 1 | << Ivar->getDeclName() << IvarType; |
1394 | 1 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
1395 | | // Fall thru - see previous comment |
1396 | 1 | } |
1397 | 195 | } |
1398 | | // __weak is explicit. So it works on Canonical type. |
1399 | 197 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()2 && |
1400 | 197 | getLangOpts().getGC() != LangOptions::NonGC2 )) { |
1401 | 2 | Diag(PropertyDiagLoc, diag::err_weak_property) |
1402 | 2 | << property->getDeclName() << Ivar->getDeclName(); |
1403 | 2 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
1404 | | // Fall thru - see previous comment |
1405 | 2 | } |
1406 | | // Fall thru - see previous comment |
1407 | 197 | if ((property->getType()->isObjCObjectPointerType() || |
1408 | 197 | PropType.isObjCGCStrong()13 ) && IvarType.isObjCGCWeak()184 && |
1409 | 197 | getLangOpts().getGC() != LangOptions::NonGC4 ) { |
1410 | 4 | Diag(PropertyDiagLoc, diag::err_strong_property) |
1411 | 4 | << property->getDeclName() << Ivar->getDeclName(); |
1412 | | // Fall thru - see previous comment |
1413 | 4 | } |
1414 | 197 | } |
1415 | 2.78k | if (getLangOpts().ObjCAutoRefCount || isARCWeak2.09k || |
1416 | 2.78k | Ivar->getType().getObjCLifetime()2.08k ) |
1417 | 706 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
1418 | 2.78k | } else if (220 PropertyIvar220 ) |
1419 | | // @dynamic |
1420 | 0 | Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl); |
1421 | | |
1422 | 0 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
1423 | 0 | ObjCPropertyImplDecl *PIDecl = |
1424 | 3.00k | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
1425 | 3.00k | property, |
1426 | 3.00k | (Synthesize ? |
1427 | 2.78k | ObjCPropertyImplDecl::Synthesize |
1428 | 3.00k | : ObjCPropertyImplDecl::Dynamic220 ), |
1429 | 3.00k | Ivar, PropertyIvarLoc); |
1430 | | |
1431 | 3.00k | if (CompleteTypeErr || !compat2.98k ) |
1432 | 20 | PIDecl->setInvalidDecl(); |
1433 | | |
1434 | 3.00k | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
1435 | 3.00k | getterMethod->createImplicitParams(Context, IDecl); |
1436 | | |
1437 | | // Redeclare the getter within the implementation as DeclContext. |
1438 | 3.00k | if (Synthesize) { |
1439 | | // If the method hasn't been overridden, create a synthesized implementation. |
1440 | 2.78k | ObjCMethodDecl *OMD = ClassImpDecl->getMethod( |
1441 | 2.78k | getterMethod->getSelector(), getterMethod->isInstanceMethod()); |
1442 | 2.78k | if (!OMD) |
1443 | 2.72k | OMD = RedeclarePropertyAccessor(Context, IC, getterMethod, AtLoc, |
1444 | 2.72k | PropertyLoc); |
1445 | 2.78k | PIDecl->setGetterMethodDecl(OMD); |
1446 | 2.78k | } |
1447 | | |
1448 | 3.00k | if (getLangOpts().CPlusPlus && Synthesize253 && !CompleteTypeErr243 && |
1449 | 3.00k | Ivar->getType()->isRecordType()240 ) { |
1450 | | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
1451 | | // returned by the getter as it must conform to C++'s copy-return rules. |
1452 | | // FIXME. Eventually we want to do this for Objective-C as well. |
1453 | 40 | SynthesizedFunctionScope Scope(*this, getterMethod); |
1454 | 40 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
1455 | 40 | DeclRefExpr *SelfExpr = new (Context) |
1456 | 40 | DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue, |
1457 | 40 | PropertyDiagLoc); |
1458 | 40 | MarkDeclRefReferenced(SelfExpr); |
1459 | 40 | Expr *LoadSelfExpr = ImplicitCastExpr::Create( |
1460 | 40 | Context, SelfDecl->getType(), CK_LValueToRValue, SelfExpr, nullptr, |
1461 | 40 | VK_PRValue, FPOptionsOverride()); |
1462 | 40 | Expr *IvarRefExpr = |
1463 | 40 | new (Context) ObjCIvarRefExpr(Ivar, |
1464 | 40 | Ivar->getUsageType(SelfDecl->getType()), |
1465 | 40 | PropertyDiagLoc, |
1466 | 40 | Ivar->getLocation(), |
1467 | 40 | LoadSelfExpr, true, true); |
1468 | 40 | ExprResult Res = PerformCopyInitialization( |
1469 | 40 | InitializedEntity::InitializeResult(PropertyDiagLoc, |
1470 | 40 | getterMethod->getReturnType()), |
1471 | 40 | PropertyDiagLoc, IvarRefExpr); |
1472 | 40 | if (!Res.isInvalid()) { |
1473 | 40 | Expr *ResExpr = Res.getAs<Expr>(); |
1474 | 40 | if (ResExpr) |
1475 | 40 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
1476 | 40 | PIDecl->setGetterCXXConstructor(ResExpr); |
1477 | 40 | } |
1478 | 40 | } |
1479 | 3.00k | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
1480 | 3.00k | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()9 ) { |
1481 | 3 | Diag(getterMethod->getLocation(), |
1482 | 3 | diag::warn_property_getter_owning_mismatch); |
1483 | 3 | Diag(property->getLocation(), diag::note_property_declare); |
1484 | 3 | } |
1485 | 3.00k | if (getLangOpts().ObjCAutoRefCount && Synthesize713 ) |
1486 | 691 | switch (getterMethod->getMethodFamily()) { |
1487 | 1 | case OMF_retain: |
1488 | 1 | case OMF_retainCount: |
1489 | 1 | case OMF_release: |
1490 | 1 | case OMF_autorelease: |
1491 | 1 | Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def) |
1492 | 1 | << 1 << getterMethod->getSelector(); |
1493 | 1 | break; |
1494 | 690 | default: |
1495 | 690 | break; |
1496 | 691 | } |
1497 | 3.00k | } |
1498 | | |
1499 | 3.00k | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
1500 | 2.61k | setterMethod->createImplicitParams(Context, IDecl); |
1501 | | |
1502 | | // Redeclare the setter within the implementation as DeclContext. |
1503 | 2.61k | if (Synthesize) { |
1504 | 2.42k | ObjCMethodDecl *OMD = ClassImpDecl->getMethod( |
1505 | 2.42k | setterMethod->getSelector(), setterMethod->isInstanceMethod()); |
1506 | 2.42k | if (!OMD) |
1507 | 2.39k | OMD = RedeclarePropertyAccessor(Context, IC, setterMethod, |
1508 | 2.39k | AtLoc, PropertyLoc); |
1509 | 2.42k | PIDecl->setSetterMethodDecl(OMD); |
1510 | 2.42k | } |
1511 | | |
1512 | 2.61k | if (getLangOpts().CPlusPlus && Synthesize210 && !CompleteTypeErr201 && |
1513 | 2.61k | Ivar->getType()->isRecordType()201 ) { |
1514 | | // FIXME. Eventually we want to do this for Objective-C as well. |
1515 | 40 | SynthesizedFunctionScope Scope(*this, setterMethod); |
1516 | 40 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
1517 | 40 | DeclRefExpr *SelfExpr = new (Context) |
1518 | 40 | DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue, |
1519 | 40 | PropertyDiagLoc); |
1520 | 40 | MarkDeclRefReferenced(SelfExpr); |
1521 | 40 | Expr *LoadSelfExpr = ImplicitCastExpr::Create( |
1522 | 40 | Context, SelfDecl->getType(), CK_LValueToRValue, SelfExpr, nullptr, |
1523 | 40 | VK_PRValue, FPOptionsOverride()); |
1524 | 40 | Expr *lhs = |
1525 | 40 | new (Context) ObjCIvarRefExpr(Ivar, |
1526 | 40 | Ivar->getUsageType(SelfDecl->getType()), |
1527 | 40 | PropertyDiagLoc, |
1528 | 40 | Ivar->getLocation(), |
1529 | 40 | LoadSelfExpr, true, true); |
1530 | 40 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
1531 | 40 | ParmVarDecl *Param = (*P); |
1532 | 40 | QualType T = Param->getType().getNonReferenceType(); |
1533 | 40 | DeclRefExpr *rhs = new (Context) |
1534 | 40 | DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc); |
1535 | 40 | MarkDeclRefReferenced(rhs); |
1536 | 40 | ExprResult Res = BuildBinOp(S, PropertyDiagLoc, |
1537 | 40 | BO_Assign, lhs, rhs); |
1538 | 40 | if (property->getPropertyAttributes() & |
1539 | 40 | ObjCPropertyAttribute::kind_atomic) { |
1540 | 30 | Expr *callExpr = Res.getAs<Expr>(); |
1541 | 30 | if (const CXXOperatorCallExpr *CXXCE = |
1542 | 30 | dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) |
1543 | 30 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
1544 | 30 | if (!FuncDecl->isTrivial()) |
1545 | 12 | if (property->getType()->isReferenceType()) { |
1546 | 3 | Diag(PropertyDiagLoc, |
1547 | 3 | diag::err_atomic_property_nontrivial_assign_op) |
1548 | 3 | << property->getType(); |
1549 | 3 | Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl) |
1550 | 3 | << FuncDecl; |
1551 | 3 | } |
1552 | 30 | } |
1553 | 40 | PIDecl->setSetterCXXAssignment(Res.getAs<Expr>()); |
1554 | 40 | } |
1555 | 2.61k | } |
1556 | | |
1557 | 3.00k | if (IC) { |
1558 | 3.00k | if (Synthesize) |
1559 | 2.78k | if (ObjCPropertyImplDecl *PPIDecl = |
1560 | 2.78k | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
1561 | 2 | Diag(PropertyLoc, diag::err_duplicate_ivar_use) |
1562 | 2 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
1563 | 2 | << PropertyIvar; |
1564 | 2 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
1565 | 2 | } |
1566 | | |
1567 | 3.00k | if (ObjCPropertyImplDecl *PPIDecl |
1568 | 3.00k | = IC->FindPropertyImplDecl(PropertyId, QueryKind)) { |
1569 | 2 | Diag(PropertyLoc, diag::err_property_implemented) << PropertyId; |
1570 | 2 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
1571 | 2 | return nullptr; |
1572 | 2 | } |
1573 | 2.99k | IC->addPropertyImplementation(PIDecl); |
1574 | 2.99k | if (getLangOpts().ObjCDefaultSynthProperties && |
1575 | 2.99k | getLangOpts().ObjCRuntime.isNonFragile()2.99k && |
1576 | 2.99k | !IDecl->isObjCRequiresPropertyDefs()2.95k ) { |
1577 | | // Diagnose if an ivar was lazily synthesdized due to a previous |
1578 | | // use and if 1) property is @dynamic or 2) property is synthesized |
1579 | | // but it requires an ivar of different name. |
1580 | 2.95k | ObjCInterfaceDecl *ClassDeclared=nullptr; |
1581 | 2.95k | ObjCIvarDecl *Ivar = nullptr; |
1582 | 2.95k | if (!Synthesize) |
1583 | 205 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
1584 | 2.74k | else { |
1585 | 2.74k | if (PropertyIvar && PropertyIvar != PropertyId) |
1586 | 1.73k | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
1587 | 2.74k | } |
1588 | | // Issue diagnostics only if Ivar belongs to current class. |
1589 | 2.95k | if (Ivar && Ivar->getSynthesize()35 && |
1590 | 2.95k | declaresSameEntity(IC->getClassInterface(), ClassDeclared)0 ) { |
1591 | 0 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
1592 | 0 | << PropertyId; |
1593 | 0 | Ivar->setInvalidDecl(); |
1594 | 0 | } |
1595 | 2.95k | } |
1596 | 2.99k | } else { |
1597 | 5 | if (Synthesize) |
1598 | 0 | if (ObjCPropertyImplDecl *PPIDecl = |
1599 | 0 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
1600 | 0 | Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use) |
1601 | 0 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
1602 | 0 | << PropertyIvar; |
1603 | 0 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
1604 | 0 | } |
1605 | | |
1606 | 5 | if (ObjCPropertyImplDecl *PPIDecl = |
1607 | 5 | CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) { |
1608 | 0 | Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId; |
1609 | 0 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
1610 | 0 | return nullptr; |
1611 | 0 | } |
1612 | 5 | CatImplClass->addPropertyImplementation(PIDecl); |
1613 | 5 | } |
1614 | | |
1615 | 3.00k | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic && |
1616 | 3.00k | PIDecl->getPropertyDecl()219 && |
1617 | 3.00k | PIDecl->getPropertyDecl()->isDirectProperty()219 ) { |
1618 | 3 | Diag(PropertyLoc, diag::err_objc_direct_dynamic_property); |
1619 | 3 | Diag(PIDecl->getPropertyDecl()->getLocation(), |
1620 | 3 | diag::note_previous_declaration); |
1621 | 3 | return nullptr; |
1622 | 3 | } |
1623 | | |
1624 | 3.00k | return PIDecl; |
1625 | 3.00k | } |
1626 | | |
1627 | | //===----------------------------------------------------------------------===// |
1628 | | // Helper methods. |
1629 | | //===----------------------------------------------------------------------===// |
1630 | | |
1631 | | /// DiagnosePropertyMismatch - Compares two properties for their |
1632 | | /// attributes and types and warns on a variety of inconsistencies. |
1633 | | /// |
1634 | | void |
1635 | | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
1636 | | ObjCPropertyDecl *SuperProperty, |
1637 | | const IdentifierInfo *inheritedName, |
1638 | 4.19k | bool OverridingProtocolProperty) { |
1639 | 4.19k | ObjCPropertyAttribute::Kind CAttr = Property->getPropertyAttributes(); |
1640 | 4.19k | ObjCPropertyAttribute::Kind SAttr = SuperProperty->getPropertyAttributes(); |
1641 | | |
1642 | | // We allow readonly properties without an explicit ownership |
1643 | | // (assign/unsafe_unretained/weak/retain/strong/copy) in super class |
1644 | | // to be overridden by a property with any explicit ownership in the subclass. |
1645 | 4.19k | if (!OverridingProtocolProperty && |
1646 | 4.19k | !getOwnershipRule(SAttr)2.79k && getOwnershipRule(CAttr)2.11k ) |
1647 | 1.63k | ; |
1648 | 2.56k | else { |
1649 | 2.56k | if ((CAttr & ObjCPropertyAttribute::kind_readonly) && |
1650 | 2.56k | (SAttr & ObjCPropertyAttribute::kind_readwrite)1.84k ) |
1651 | 1 | Diag(Property->getLocation(), diag::warn_readonly_property) |
1652 | 1 | << Property->getDeclName() << inheritedName; |
1653 | 2.56k | if ((CAttr & ObjCPropertyAttribute::kind_copy) != |
1654 | 2.56k | (SAttr & ObjCPropertyAttribute::kind_copy)) |
1655 | 8 | Diag(Property->getLocation(), diag::warn_property_attribute) |
1656 | 8 | << Property->getDeclName() << "copy" << inheritedName; |
1657 | 2.55k | else if (!(SAttr & ObjCPropertyAttribute::kind_readonly)) { |
1658 | 189 | unsigned CAttrRetain = (CAttr & (ObjCPropertyAttribute::kind_retain | |
1659 | 189 | ObjCPropertyAttribute::kind_strong)); |
1660 | 189 | unsigned SAttrRetain = (SAttr & (ObjCPropertyAttribute::kind_retain | |
1661 | 189 | ObjCPropertyAttribute::kind_strong)); |
1662 | 189 | bool CStrong = (CAttrRetain != 0); |
1663 | 189 | bool SStrong = (SAttrRetain != 0); |
1664 | 189 | if (CStrong != SStrong) |
1665 | 0 | Diag(Property->getLocation(), diag::warn_property_attribute) |
1666 | 0 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
1667 | 189 | } |
1668 | 2.56k | } |
1669 | | |
1670 | | // Check for nonatomic; note that nonatomic is effectively |
1671 | | // meaningless for readonly properties, so don't diagnose if the |
1672 | | // atomic property is 'readonly'. |
1673 | 4.19k | checkAtomicPropertyMismatch(*this, SuperProperty, Property, false); |
1674 | | // Readonly properties from protocols can be implemented as "readwrite" |
1675 | | // with a custom setter name. |
1676 | 4.19k | if (Property->getSetterName() != SuperProperty->getSetterName() && |
1677 | 4.19k | !(2 SuperProperty->isReadOnly()2 && |
1678 | 2 | isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) { |
1679 | 1 | Diag(Property->getLocation(), diag::warn_property_attribute) |
1680 | 1 | << Property->getDeclName() << "setter" << inheritedName; |
1681 | 1 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
1682 | 1 | } |
1683 | 4.19k | if (Property->getGetterName() != SuperProperty->getGetterName()) { |
1684 | 0 | Diag(Property->getLocation(), diag::warn_property_attribute) |
1685 | 0 | << Property->getDeclName() << "getter" << inheritedName; |
1686 | 0 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
1687 | 0 | } |
1688 | | |
1689 | 4.19k | QualType LHSType = |
1690 | 4.19k | Context.getCanonicalType(SuperProperty->getType()); |
1691 | 4.19k | QualType RHSType = |
1692 | 4.19k | Context.getCanonicalType(Property->getType()); |
1693 | | |
1694 | 4.19k | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
1695 | | // Do cases not handled in above. |
1696 | | // FIXME. For future support of covariant property types, revisit this. |
1697 | 34 | bool IncompatibleObjC = false; |
1698 | 34 | QualType ConvertedType; |
1699 | 34 | if (!isObjCPointerConversion(RHSType, LHSType, |
1700 | 34 | ConvertedType, IncompatibleObjC) || |
1701 | 34 | IncompatibleObjC15 ) { |
1702 | 23 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
1703 | 23 | << Property->getType() << SuperProperty->getType() << inheritedName; |
1704 | 23 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
1705 | 23 | } |
1706 | 34 | } |
1707 | 4.19k | } |
1708 | | |
1709 | | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
1710 | | ObjCMethodDecl *GetterMethod, |
1711 | 352k | SourceLocation Loc) { |
1712 | 352k | if (!GetterMethod) |
1713 | 351k | return false; |
1714 | 1.67k | QualType GetterType = GetterMethod->getReturnType().getNonReferenceType(); |
1715 | 1.67k | QualType PropertyRValueType = |
1716 | 1.67k | property->getType().getNonReferenceType().getAtomicUnqualifiedType(); |
1717 | 1.67k | bool compat = Context.hasSameType(PropertyRValueType, GetterType); |
1718 | 1.67k | if (!compat) { |
1719 | 30 | const ObjCObjectPointerType *propertyObjCPtr = nullptr; |
1720 | 30 | const ObjCObjectPointerType *getterObjCPtr = nullptr; |
1721 | 30 | if ((propertyObjCPtr = |
1722 | 30 | PropertyRValueType->getAs<ObjCObjectPointerType>()) && |
1723 | 30 | (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>())24 ) |
1724 | 24 | compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr); |
1725 | 6 | else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType) |
1726 | 6 | != Compatible) { |
1727 | 2 | Diag(Loc, diag::err_property_accessor_type) |
1728 | 2 | << property->getDeclName() << PropertyRValueType |
1729 | 2 | << GetterMethod->getSelector() << GetterType; |
1730 | 2 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
1731 | 2 | return true; |
1732 | 4 | } else { |
1733 | 4 | compat = true; |
1734 | 4 | QualType lhsType = Context.getCanonicalType(PropertyRValueType); |
1735 | 4 | QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); |
1736 | 4 | if (lhsType != rhsType && lhsType->isArithmeticType()2 ) |
1737 | 2 | compat = false; |
1738 | 4 | } |
1739 | 30 | } |
1740 | | |
1741 | 1.67k | if (!compat) { |
1742 | 5 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
1743 | 5 | << property->getDeclName() |
1744 | 5 | << GetterMethod->getSelector(); |
1745 | 5 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
1746 | 5 | return true; |
1747 | 5 | } |
1748 | | |
1749 | 1.66k | return false; |
1750 | 1.67k | } |
1751 | | |
1752 | | /// CollectImmediateProperties - This routine collects all properties in |
1753 | | /// the class and its conforming protocols; but not those in its super class. |
1754 | | static void |
1755 | | CollectImmediateProperties(ObjCContainerDecl *CDecl, |
1756 | | ObjCContainerDecl::PropertyMap &PropMap, |
1757 | | ObjCContainerDecl::PropertyMap &SuperPropMap, |
1758 | | bool CollectClassPropsOnly = false, |
1759 | 6.70k | bool IncludeProtocols = true) { |
1760 | 6.70k | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
1761 | 4.86k | for (auto *Prop : IDecl->properties()) { |
1762 | 2.79k | if (CollectClassPropsOnly && !Prop->isClassProperty()2.72k ) |
1763 | 2.67k | continue; |
1764 | 119 | PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = |
1765 | 119 | Prop; |
1766 | 119 | } |
1767 | | |
1768 | | // Collect the properties from visible extensions. |
1769 | 4.86k | for (auto *Ext : IDecl->visible_extensions()) |
1770 | 478 | CollectImmediateProperties(Ext, PropMap, SuperPropMap, |
1771 | 478 | CollectClassPropsOnly, IncludeProtocols); |
1772 | | |
1773 | 4.86k | if (IncludeProtocols) { |
1774 | | // Scan through class's protocols. |
1775 | 4.85k | for (auto *PI : IDecl->all_referenced_protocols()) |
1776 | 549 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
1777 | 549 | CollectClassPropsOnly); |
1778 | 4.85k | } |
1779 | 4.86k | } |
1780 | 6.70k | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
1781 | 984 | for (auto *Prop : CATDecl->properties()) { |
1782 | 252 | if (CollectClassPropsOnly && !Prop->isClassProperty()230 ) |
1783 | 230 | continue; |
1784 | 22 | PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = |
1785 | 22 | Prop; |
1786 | 22 | } |
1787 | 984 | if (IncludeProtocols) { |
1788 | | // Scan through class's protocols. |
1789 | 984 | for (auto *PI : CATDecl->protocols()) |
1790 | 125 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
1791 | 125 | CollectClassPropsOnly); |
1792 | 984 | } |
1793 | 984 | } |
1794 | 5.72k | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
1795 | 861 | for (auto *Prop : PDecl->properties()) { |
1796 | 249 | if (CollectClassPropsOnly && !Prop->isClassProperty()222 ) |
1797 | 197 | continue; |
1798 | 52 | ObjCPropertyDecl *PropertyFromSuper = |
1799 | 52 | SuperPropMap[std::make_pair(Prop->getIdentifier(), |
1800 | 52 | Prop->isClassProperty())]; |
1801 | | // Exclude property for protocols which conform to class's super-class, |
1802 | | // as super-class has to implement the property. |
1803 | 52 | if (!PropertyFromSuper || |
1804 | 52 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()9 ) { |
1805 | 43 | ObjCPropertyDecl *&PropEntry = |
1806 | 43 | PropMap[std::make_pair(Prop->getIdentifier(), |
1807 | 43 | Prop->isClassProperty())]; |
1808 | 43 | if (!PropEntry) |
1809 | 43 | PropEntry = Prop; |
1810 | 43 | } |
1811 | 52 | } |
1812 | | // Scan through protocol's protocols. |
1813 | 861 | for (auto *PI : PDecl->protocols()) |
1814 | 187 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
1815 | 187 | CollectClassPropsOnly); |
1816 | 861 | } |
1817 | 6.70k | } |
1818 | | |
1819 | | /// CollectSuperClassPropertyImplementations - This routine collects list of |
1820 | | /// properties to be implemented in super class(s) and also coming from their |
1821 | | /// conforming protocols. |
1822 | | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
1823 | 6.80k | ObjCInterfaceDecl::PropertyMap &PropMap) { |
1824 | 6.80k | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
1825 | 3.14k | ObjCInterfaceDecl::PropertyDeclOrder PO; |
1826 | 6.83k | while (SDecl) { |
1827 | 3.69k | SDecl->collectPropertiesToImplement(PropMap, PO); |
1828 | 3.69k | SDecl = SDecl->getSuperClass(); |
1829 | 3.69k | } |
1830 | 3.14k | } |
1831 | 6.80k | } |
1832 | | |
1833 | | /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is |
1834 | | /// an ivar synthesized for 'Method' and 'Method' is a property accessor |
1835 | | /// declared in class 'IFace'. |
1836 | | bool |
1837 | | Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, |
1838 | 1.98k | ObjCMethodDecl *Method, ObjCIvarDecl *IV) { |
1839 | 1.98k | if (!IV->getSynthesize()) |
1840 | 1.68k | return false; |
1841 | 298 | ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(), |
1842 | 298 | Method->isInstanceMethod()); |
1843 | 298 | if (!IMD || !IMD->isPropertyAccessor()83 ) |
1844 | 244 | return false; |
1845 | | |
1846 | | // look up a property declaration whose one of its accessors is implemented |
1847 | | // by this method. |
1848 | 156 | for (const auto *Property : IFace->instance_properties())54 { |
1849 | 156 | if ((Property->getGetterName() == IMD->getSelector() || |
1850 | 156 | Property->getSetterName() == IMD->getSelector()139 ) && |
1851 | 156 | (Property->getPropertyIvarDecl() == IV)44 ) |
1852 | 33 | return true; |
1853 | 156 | } |
1854 | | // Also look up property declaration in class extension whose one of its |
1855 | | // accessors is implemented by this method. |
1856 | 21 | for (const auto *Ext : IFace->known_extensions()) |
1857 | 2 | for (const auto *Property : Ext->instance_properties()) |
1858 | 2 | if ((Property->getGetterName() == IMD->getSelector() || |
1859 | 2 | Property->getSetterName() == IMD->getSelector()1 ) && |
1860 | 2 | (Property->getPropertyIvarDecl() == IV)1 ) |
1861 | 1 | return true; |
1862 | 20 | return false; |
1863 | 21 | } |
1864 | | |
1865 | | static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl, |
1866 | 34 | ObjCPropertyDecl *Prop) { |
1867 | 34 | bool SuperClassImplementsGetter = false; |
1868 | 34 | bool SuperClassImplementsSetter = false; |
1869 | 34 | if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) |
1870 | 15 | SuperClassImplementsSetter = true; |
1871 | | |
1872 | 58 | while (IDecl->getSuperClass()) { |
1873 | 37 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
1874 | 37 | if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName())35 ) |
1875 | 13 | SuperClassImplementsGetter = true; |
1876 | | |
1877 | 37 | if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName())18 ) |
1878 | 9 | SuperClassImplementsSetter = true; |
1879 | 37 | if (SuperClassImplementsGetter && SuperClassImplementsSetter15 ) |
1880 | 13 | return true; |
1881 | 24 | IDecl = IDecl->getSuperClass(); |
1882 | 24 | } |
1883 | 21 | return false; |
1884 | 34 | } |
1885 | | |
1886 | | /// Default synthesizes all properties which must be synthesized |
1887 | | /// in class's \@implementation. |
1888 | | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl, |
1889 | | ObjCInterfaceDecl *IDecl, |
1890 | 4.60k | SourceLocation AtEnd) { |
1891 | 4.60k | ObjCInterfaceDecl::PropertyMap PropMap; |
1892 | 4.60k | ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder; |
1893 | 4.60k | IDecl->collectPropertiesToImplement(PropMap, PropertyOrder); |
1894 | 4.60k | if (PropMap.empty()) |
1895 | 3.16k | return; |
1896 | 1.44k | ObjCInterfaceDecl::PropertyMap SuperPropMap; |
1897 | 1.44k | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
1898 | | |
1899 | 4.62k | for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++3.17k ) { |
1900 | 3.17k | ObjCPropertyDecl *Prop = PropertyOrder[i]; |
1901 | | // Is there a matching property synthesize/dynamic? |
1902 | 3.17k | if (Prop->isInvalidDecl() || |
1903 | 3.17k | Prop->isClassProperty()3.10k || |
1904 | 3.17k | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional3.02k ) |
1905 | 168 | continue; |
1906 | | // Property may have been synthesized by user. |
1907 | 3.00k | if (IMPDecl->FindPropertyImplDecl( |
1908 | 3.00k | Prop->getIdentifier(), Prop->getQueryKind())) |
1909 | 1.61k | continue; |
1910 | 1.39k | ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Prop->getGetterName()); |
1911 | 1.39k | if (ImpMethod && !ImpMethod->getBody()138 ) { |
1912 | 138 | if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) |
1913 | 43 | continue; |
1914 | 95 | ImpMethod = IMPDecl->getInstanceMethod(Prop->getSetterName()); |
1915 | 95 | if (ImpMethod && !ImpMethod->getBody()43 ) |
1916 | 43 | continue; |
1917 | 95 | } |
1918 | 1.30k | if (ObjCPropertyImplDecl *PID = |
1919 | 1.30k | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) { |
1920 | 2 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property) |
1921 | 2 | << Prop->getIdentifier(); |
1922 | 2 | if (PID->getLocation().isValid()) |
1923 | 2 | Diag(PID->getLocation(), diag::note_property_synthesize); |
1924 | 2 | continue; |
1925 | 2 | } |
1926 | 1.30k | ObjCPropertyDecl *PropInSuperClass = |
1927 | 1.30k | SuperPropMap[std::make_pair(Prop->getIdentifier(), |
1928 | 1.30k | Prop->isClassProperty())]; |
1929 | 1.30k | if (ObjCProtocolDecl *Proto = |
1930 | 1.30k | dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) { |
1931 | | // We won't auto-synthesize properties declared in protocols. |
1932 | | // Suppress the warning if class's superclass implements property's |
1933 | | // getter and implements property's setter (if readwrite property). |
1934 | | // Or, if property is going to be implemented in its super class. |
1935 | 34 | if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass21 ) { |
1936 | 15 | Diag(IMPDecl->getLocation(), |
1937 | 15 | diag::warn_auto_synthesizing_protocol_property) |
1938 | 15 | << Prop << Proto; |
1939 | 15 | Diag(Prop->getLocation(), diag::note_property_declare); |
1940 | 15 | std::string FixIt = |
1941 | 15 | (Twine("@synthesize ") + Prop->getName() + ";\n\n").str(); |
1942 | 15 | Diag(AtEnd, diag::note_add_synthesize_directive) |
1943 | 15 | << FixItHint::CreateInsertion(AtEnd, FixIt); |
1944 | 15 | } |
1945 | 34 | continue; |
1946 | 34 | } |
1947 | | // If property to be implemented in the super class, ignore. |
1948 | 1.27k | if (PropInSuperClass) { |
1949 | 26 | if ((Prop->getPropertyAttributes() & |
1950 | 26 | ObjCPropertyAttribute::kind_readwrite) && |
1951 | 26 | (PropInSuperClass->getPropertyAttributes() & |
1952 | 24 | ObjCPropertyAttribute::kind_readonly) && |
1953 | 26 | !IMPDecl->getInstanceMethod(Prop->getSetterName())11 && |
1954 | 26 | !IDecl->HasUserDeclaredSetterMethod(Prop)9 ) { |
1955 | 3 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property) |
1956 | 3 | << Prop->getIdentifier(); |
1957 | 3 | Diag(PropInSuperClass->getLocation(), diag::note_property_declare); |
1958 | 23 | } else { |
1959 | 23 | Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass) |
1960 | 23 | << Prop->getIdentifier(); |
1961 | 23 | Diag(PropInSuperClass->getLocation(), diag::note_property_declare); |
1962 | 23 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
1963 | 23 | } |
1964 | 26 | continue; |
1965 | 26 | } |
1966 | | // We use invalid SourceLocations for the synthesized ivars since they |
1967 | | // aren't really synthesized at a particular location; they just exist. |
1968 | | // Saying that they are located at the @implementation isn't really going |
1969 | | // to help users. |
1970 | 1.24k | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
1971 | 1.24k | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
1972 | 1.24k | true, |
1973 | 1.24k | /* property = */ Prop->getIdentifier(), |
1974 | 1.24k | /* ivar = */ Prop->getDefaultSynthIvarName(Context), |
1975 | 1.24k | Prop->getLocation(), Prop->getQueryKind())); |
1976 | 1.24k | if (PIDecl && !Prop->isUnavailable()) { |
1977 | 1.23k | Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); |
1978 | 1.23k | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
1979 | 1.23k | } |
1980 | 1.24k | } |
1981 | 1.44k | } |
1982 | | |
1983 | | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D, |
1984 | 5.37k | SourceLocation AtEnd) { |
1985 | 5.37k | if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()5.23k ) |
1986 | 370 | return; |
1987 | 5.00k | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
1988 | 5.00k | if (!IC) |
1989 | 389 | return; |
1990 | 4.61k | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
1991 | 4.61k | if (!IDecl->isObjCRequiresPropertyDefs()) |
1992 | 4.60k | DefaultSynthesizeProperties(S, IC, IDecl, AtEnd); |
1993 | 4.61k | } |
1994 | | |
1995 | | static void DiagnoseUnimplementedAccessor( |
1996 | | Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method, |
1997 | | ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C, |
1998 | | ObjCPropertyDecl *Prop, |
1999 | 182 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) { |
2000 | | // Check to see if we have a corresponding selector in SMap and with the |
2001 | | // right method type. |
2002 | 357 | auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) { |
2003 | 357 | return x->getSelector() == Method && |
2004 | 357 | x->isClassMethod() == Prop->isClassProperty()59 ; |
2005 | 357 | }); |
2006 | | // When reporting on missing property setter/getter implementation in |
2007 | | // categories, do not report when they are declared in primary class, |
2008 | | // class's protocol, or one of it super classes. This is because, |
2009 | | // the class is going to implement them. |
2010 | 182 | if (I == SMap.end() && |
2011 | 182 | (133 PrimaryClass == nullptr133 || |
2012 | 133 | !PrimaryClass->lookupPropertyAccessor(Method, C, |
2013 | 124 | Prop->isClassProperty()))) { |
2014 | 124 | unsigned diag = |
2015 | 124 | isa<ObjCCategoryDecl>(CDecl) |
2016 | 124 | ? (7 Prop->isClassProperty()7 |
2017 | 7 | ? diag::warn_impl_required_in_category_for_class_property0 |
2018 | 7 | : diag::warn_setter_getter_impl_required_in_category) |
2019 | 124 | : (117 Prop->isClassProperty()117 |
2020 | 117 | ? diag::warn_impl_required_for_class_property86 |
2021 | 117 | : diag::warn_setter_getter_impl_required31 ); |
2022 | 124 | S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method; |
2023 | 124 | S.Diag(Prop->getLocation(), diag::note_property_declare); |
2024 | 124 | if (S.LangOpts.ObjCDefaultSynthProperties && |
2025 | 124 | S.LangOpts.ObjCRuntime.isNonFragile()112 ) |
2026 | 104 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
2027 | 97 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
2028 | 10 | S.Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
2029 | 124 | } |
2030 | 182 | } |
2031 | | |
2032 | | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
2033 | | ObjCContainerDecl *CDecl, |
2034 | 5.35k | bool SynthesizeProperties) { |
2035 | 5.35k | ObjCContainerDecl::PropertyMap PropMap; |
2036 | 5.35k | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
2037 | | |
2038 | | // Since we don't synthesize class properties, we should emit diagnose even |
2039 | | // if SynthesizeProperties is true. |
2040 | 5.35k | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
2041 | | // Gather properties which need not be implemented in this class |
2042 | | // or category. |
2043 | 5.35k | if (!IDecl) |
2044 | 506 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
2045 | | // For categories, no need to implement properties declared in |
2046 | | // its primary class (and its super classes) if property is |
2047 | | // declared in one of those containers. |
2048 | 506 | if ((IDecl = C->getClassInterface())) { |
2049 | 506 | ObjCInterfaceDecl::PropertyDeclOrder PO; |
2050 | 506 | IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); |
2051 | 506 | } |
2052 | 506 | } |
2053 | 5.35k | if (IDecl) |
2054 | 5.35k | CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); |
2055 | | |
2056 | | // When SynthesizeProperties is true, we only check class properties. |
2057 | 5.35k | CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap, |
2058 | 5.35k | SynthesizeProperties/*CollectClassPropsOnly*/); |
2059 | | |
2060 | | // Scan the @interface to see if any of the protocols it adopts |
2061 | | // require an explicit implementation, via attribute |
2062 | | // 'objc_protocol_requires_explicit_implementation'. |
2063 | 5.35k | if (IDecl) { |
2064 | 5.35k | std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap; |
2065 | | |
2066 | 5.35k | for (auto *PDecl : IDecl->all_referenced_protocols()) { |
2067 | 655 | if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
2068 | 646 | continue; |
2069 | | // Lazily construct a set of all the properties in the @interface |
2070 | | // of the class, without looking at the superclass. We cannot |
2071 | | // use the call to CollectImmediateProperties() above as that |
2072 | | // utilizes information from the super class's properties as well |
2073 | | // as scans the adopted protocols. This work only triggers for protocols |
2074 | | // with the attribute, which is very rare, and only occurs when |
2075 | | // analyzing the @implementation. |
2076 | 9 | if (!LazyMap) { |
2077 | 9 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
2078 | 9 | LazyMap.reset(new ObjCContainerDecl::PropertyMap()); |
2079 | 9 | CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap, |
2080 | 9 | /* CollectClassPropsOnly */ false, |
2081 | 9 | /* IncludeProtocols */ false); |
2082 | 9 | } |
2083 | | // Add the properties of 'PDecl' to the list of properties that |
2084 | | // need to be implemented. |
2085 | 9 | for (auto *PropDecl : PDecl->properties()) { |
2086 | 3 | if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(), |
2087 | 3 | PropDecl->isClassProperty())]) |
2088 | 1 | continue; |
2089 | 2 | PropMap[std::make_pair(PropDecl->getIdentifier(), |
2090 | 2 | PropDecl->isClassProperty())] = PropDecl; |
2091 | 2 | } |
2092 | 9 | } |
2093 | 5.35k | } |
2094 | | |
2095 | 5.35k | if (PropMap.empty()) |
2096 | 5.24k | return; |
2097 | | |
2098 | 110 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
2099 | 110 | for (const auto *I : IMPDecl->property_impls()) |
2100 | 79 | PropImplMap.insert(I->getPropertyDecl()); |
2101 | | |
2102 | 110 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap; |
2103 | | // Collect property accessors implemented in current implementation. |
2104 | 110 | for (const auto *I : IMPDecl->methods()) |
2105 | 243 | InsMap.insert(I); |
2106 | | |
2107 | 110 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
2108 | 110 | ObjCInterfaceDecl *PrimaryClass = nullptr; |
2109 | 110 | if (C && !C->IsClassExtension()23 ) |
2110 | 23 | if ((PrimaryClass = C->getClassInterface())) |
2111 | | // Report unimplemented properties in the category as well. |
2112 | 23 | if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { |
2113 | | // When reporting on missing setter/getters, do not report when |
2114 | | // setter/getter is implemented in category's primary class |
2115 | | // implementation. |
2116 | 7 | for (const auto *I : IMP->methods()) |
2117 | 29 | InsMap.insert(I); |
2118 | 7 | } |
2119 | | |
2120 | 110 | for (ObjCContainerDecl::PropertyMap::iterator |
2121 | 293 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P183 ) { |
2122 | 183 | ObjCPropertyDecl *Prop = P->second; |
2123 | | // Is there a matching property synthesize/dynamic? |
2124 | 183 | if (Prop->isInvalidDecl() || |
2125 | 183 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
2126 | 183 | PropImplMap.count(Prop)182 || |
2127 | 183 | Prop->getAvailability() == AR_Unavailable123 ) |
2128 | 60 | continue; |
2129 | | |
2130 | | // Diagnose unimplemented getters and setters. |
2131 | 123 | DiagnoseUnimplementedAccessor(*this, |
2132 | 123 | PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap); |
2133 | 123 | if (!Prop->isReadOnly()) |
2134 | 59 | DiagnoseUnimplementedAccessor(*this, |
2135 | 59 | PrimaryClass, Prop->getSetterName(), |
2136 | 59 | IMPDecl, CDecl, C, Prop, InsMap); |
2137 | 123 | } |
2138 | 110 | } |
2139 | | |
2140 | 5.35k | void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) { |
2141 | 5.35k | for (const auto *propertyImpl : impDecl->property_impls()) { |
2142 | 3.00k | const auto *property = propertyImpl->getPropertyDecl(); |
2143 | | // Warn about null_resettable properties with synthesized setters, |
2144 | | // because the setter won't properly handle nil. |
2145 | 3.00k | if (propertyImpl->getPropertyImplementation() == |
2146 | 3.00k | ObjCPropertyImplDecl::Synthesize && |
2147 | 3.00k | (property->getPropertyAttributes() & |
2148 | 2.78k | ObjCPropertyAttribute::kind_null_resettable) && |
2149 | 3.00k | property->getGetterMethodDecl()5 && property->getSetterMethodDecl()5 ) { |
2150 | 5 | auto *getterImpl = propertyImpl->getGetterMethodDecl(); |
2151 | 5 | auto *setterImpl = propertyImpl->getSetterMethodDecl(); |
2152 | 5 | if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) && |
2153 | 5 | (4 !setterImpl4 || setterImpl->isSynthesizedAccessorStub()4 )) { |
2154 | 2 | SourceLocation loc = propertyImpl->getLocation(); |
2155 | 2 | if (loc.isInvalid()) |
2156 | 1 | loc = impDecl->getBeginLoc(); |
2157 | | |
2158 | 2 | Diag(loc, diag::warn_null_resettable_setter) |
2159 | 2 | << setterImpl->getSelector() << property->getDeclName(); |
2160 | 2 | } |
2161 | 5 | } |
2162 | 3.00k | } |
2163 | 5.35k | } |
2164 | | |
2165 | | void |
2166 | | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
2167 | 4.85k | ObjCInterfaceDecl* IDecl) { |
2168 | | // Rules apply in non-GC mode only |
2169 | 4.85k | if (getLangOpts().getGC() != LangOptions::NonGC) |
2170 | 69 | return; |
2171 | 4.78k | ObjCContainerDecl::PropertyMap PM; |
2172 | 4.78k | for (auto *Prop : IDecl->properties()) |
2173 | 2.75k | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; |
2174 | 4.78k | for (const auto *Ext : IDecl->known_extensions()) |
2175 | 476 | for (auto *Prop : Ext->properties()) |
2176 | 226 | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; |
2177 | | |
2178 | 4.78k | for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end(); |
2179 | 7.69k | I != E; ++I2.91k ) { |
2180 | 2.91k | const ObjCPropertyDecl *Property = I->second; |
2181 | 2.91k | ObjCMethodDecl *GetterMethod = nullptr; |
2182 | 2.91k | ObjCMethodDecl *SetterMethod = nullptr; |
2183 | | |
2184 | 2.91k | unsigned Attributes = Property->getPropertyAttributes(); |
2185 | 2.91k | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
2186 | | |
2187 | 2.91k | if (!(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic) && |
2188 | 2.91k | !(AttributesAsWritten & ObjCPropertyAttribute::kind_nonatomic)2.88k ) { |
2189 | 2.37k | GetterMethod = Property->isClassProperty() ? |
2190 | 57 | IMPDecl->getClassMethod(Property->getGetterName()) : |
2191 | 2.37k | IMPDecl->getInstanceMethod(Property->getGetterName())2.31k ; |
2192 | 2.37k | SetterMethod = Property->isClassProperty() ? |
2193 | 57 | IMPDecl->getClassMethod(Property->getSetterName()) : |
2194 | 2.37k | IMPDecl->getInstanceMethod(Property->getSetterName())2.31k ; |
2195 | 2.37k | if (GetterMethod && GetterMethod->isSynthesizedAccessorStub()2.13k ) |
2196 | 1.99k | GetterMethod = nullptr; |
2197 | 2.37k | if (SetterMethod && SetterMethod->isSynthesizedAccessorStub()1.89k ) |
2198 | 1.81k | SetterMethod = nullptr; |
2199 | 2.37k | if (GetterMethod) { |
2200 | 139 | Diag(GetterMethod->getLocation(), |
2201 | 139 | diag::warn_default_atomic_custom_getter_setter) |
2202 | 139 | << Property->getIdentifier() << 0; |
2203 | 139 | Diag(Property->getLocation(), diag::note_property_declare); |
2204 | 139 | } |
2205 | 2.37k | if (SetterMethod) { |
2206 | 86 | Diag(SetterMethod->getLocation(), |
2207 | 86 | diag::warn_default_atomic_custom_getter_setter) |
2208 | 86 | << Property->getIdentifier() << 1; |
2209 | 86 | Diag(Property->getLocation(), diag::note_property_declare); |
2210 | 86 | } |
2211 | 2.37k | } |
2212 | | |
2213 | | // We only care about readwrite atomic property. |
2214 | 2.91k | if ((Attributes & ObjCPropertyAttribute::kind_nonatomic) || |
2215 | 2.91k | !(Attributes & ObjCPropertyAttribute::kind_readwrite)2.39k ) |
2216 | 784 | continue; |
2217 | 2.12k | if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl( |
2218 | 2.12k | Property->getIdentifier(), Property->getQueryKind())) { |
2219 | 2.01k | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
2220 | 162 | continue; |
2221 | 1.85k | GetterMethod = PIDecl->getGetterMethodDecl(); |
2222 | 1.85k | SetterMethod = PIDecl->getSetterMethodDecl(); |
2223 | 1.85k | if (GetterMethod && GetterMethod->isSynthesizedAccessorStub()) |
2224 | 1.82k | GetterMethod = nullptr; |
2225 | 1.85k | if (SetterMethod && SetterMethod->isSynthesizedAccessorStub()) |
2226 | 1.83k | SetterMethod = nullptr; |
2227 | 1.85k | if ((bool)GetterMethod ^ (bool)SetterMethod) { |
2228 | 20 | SourceLocation MethodLoc = |
2229 | 20 | (GetterMethod ? GetterMethod->getLocation()12 |
2230 | 20 | : SetterMethod->getLocation()8 ); |
2231 | 20 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
2232 | 20 | << Property->getIdentifier() << (GetterMethod != nullptr) |
2233 | 20 | << (SetterMethod != nullptr); |
2234 | | // fixit stuff. |
2235 | 20 | if (Property->getLParenLoc().isValid() && |
2236 | 20 | !(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic)13 ) { |
2237 | | // @property () ... case. |
2238 | 11 | SourceLocation AfterLParen = |
2239 | 11 | getLocForEndOfToken(Property->getLParenLoc()); |
2240 | 11 | StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "10 |
2241 | 11 | : "nonatomic"1 ; |
2242 | 11 | Diag(Property->getLocation(), |
2243 | 11 | diag::note_atomic_property_fixup_suggest) |
2244 | 11 | << FixItHint::CreateInsertion(AfterLParen, NonatomicStr); |
2245 | 11 | } else if (9 Property->getLParenLoc().isInvalid()9 ) { |
2246 | | //@property id etc. |
2247 | 7 | SourceLocation startLoc = |
2248 | 7 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
2249 | 7 | Diag(Property->getLocation(), |
2250 | 7 | diag::note_atomic_property_fixup_suggest) |
2251 | 7 | << FixItHint::CreateInsertion(startLoc, "(nonatomic) "); |
2252 | 7 | } else |
2253 | 2 | Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); |
2254 | 20 | Diag(Property->getLocation(), diag::note_property_declare); |
2255 | 20 | } |
2256 | 1.85k | } |
2257 | 2.12k | } |
2258 | 4.78k | } |
2259 | | |
2260 | 4.85k | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
2261 | 4.85k | if (getLangOpts().getGC() == LangOptions::GCOnly) |
2262 | 6 | return; |
2263 | | |
2264 | 4.84k | for (const auto *PID : D->property_impls()) { |
2265 | 2.99k | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
2266 | 2.99k | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
2267 | 2.99k | !PD->isClassProperty()2.98k ) { |
2268 | 2.98k | ObjCMethodDecl *IM = PID->getGetterMethodDecl(); |
2269 | 2.98k | if (IM && !IM->isSynthesizedAccessorStub()2.77k ) |
2270 | 104 | continue; |
2271 | 2.87k | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
2272 | 2.87k | if (!method) |
2273 | 0 | continue; |
2274 | 2.87k | ObjCMethodFamily family = method->getMethodFamily(); |
2275 | 2.87k | if (family == OMF_alloc || family == OMF_copy2.87k || |
2276 | 2.87k | family == OMF_mutableCopy2.87k || family == OMF_new2.87k ) { |
2277 | 11 | if (getLangOpts().ObjCAutoRefCount) |
2278 | 11 | Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule); |
2279 | 0 | else |
2280 | 0 | Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule); |
2281 | | |
2282 | | // Look for a getter explicitly declared alongside the property. |
2283 | | // If we find one, use its location for the note. |
2284 | 11 | SourceLocation noteLoc = PD->getLocation(); |
2285 | 11 | SourceLocation fixItLoc; |
2286 | 19 | for (auto *getterRedecl : method->redecls()) { |
2287 | 19 | if (getterRedecl->isImplicit()) |
2288 | 15 | continue; |
2289 | 4 | if (getterRedecl->getDeclContext() != PD->getDeclContext()) |
2290 | 2 | continue; |
2291 | 2 | noteLoc = getterRedecl->getLocation(); |
2292 | 2 | fixItLoc = getterRedecl->getEndLoc(); |
2293 | 2 | } |
2294 | | |
2295 | 11 | Preprocessor &PP = getPreprocessor(); |
2296 | 11 | TokenValue tokens[] = { |
2297 | 11 | tok::kw___attribute, tok::l_paren, tok::l_paren, |
2298 | 11 | PP.getIdentifierInfo("objc_method_family"), tok::l_paren, |
2299 | 11 | PP.getIdentifierInfo("none"), tok::r_paren, |
2300 | 11 | tok::r_paren, tok::r_paren |
2301 | 11 | }; |
2302 | 11 | StringRef spelling = "__attribute__((objc_method_family(none)))"; |
2303 | 11 | StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens); |
2304 | 11 | if (!macroName.empty()) |
2305 | 2 | spelling = macroName; |
2306 | | |
2307 | 11 | auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family) |
2308 | 11 | << method->getDeclName() << spelling; |
2309 | 11 | if (fixItLoc.isValid()) { |
2310 | 2 | SmallString<64> fixItText(" "); |
2311 | 2 | fixItText += spelling; |
2312 | 2 | noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText); |
2313 | 2 | } |
2314 | 11 | } |
2315 | 2.87k | } |
2316 | 2.99k | } |
2317 | 4.84k | } |
2318 | | |
2319 | | void Sema::DiagnoseMissingDesignatedInitOverrides( |
2320 | | const ObjCImplementationDecl *ImplD, |
2321 | 14 | const ObjCInterfaceDecl *IFD) { |
2322 | 14 | assert(IFD->hasDesignatedInitializers()); |
2323 | 0 | const ObjCInterfaceDecl *SuperD = IFD->getSuperClass(); |
2324 | 14 | if (!SuperD) |
2325 | 3 | return; |
2326 | | |
2327 | 11 | SelectorSet InitSelSet; |
2328 | 11 | for (const auto *I : ImplD->instance_methods()) |
2329 | 22 | if (I->getMethodFamily() == OMF_init) |
2330 | 22 | InitSelSet.insert(I->getSelector()); |
2331 | | |
2332 | 11 | SmallVector<const ObjCMethodDecl *, 8> DesignatedInits; |
2333 | 11 | SuperD->getDesignatedInitializers(DesignatedInits); |
2334 | 11 | for (SmallVector<const ObjCMethodDecl *, 8>::iterator |
2335 | 22 | I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I11 ) { |
2336 | 11 | const ObjCMethodDecl *MD = *I; |
2337 | 11 | if (!InitSelSet.count(MD->getSelector())) { |
2338 | | // Don't emit a diagnostic if the overriding method in the subclass is |
2339 | | // marked as unavailable. |
2340 | 7 | bool Ignore = false; |
2341 | 7 | if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) { |
2342 | 1 | Ignore = IMD->isUnavailable(); |
2343 | 6 | } else { |
2344 | | // Check the methods declared in the class extensions too. |
2345 | 6 | for (auto *Ext : IFD->visible_extensions()) |
2346 | 1 | if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) { |
2347 | 1 | Ignore = IMD->isUnavailable(); |
2348 | 1 | break; |
2349 | 1 | } |
2350 | 6 | } |
2351 | 7 | if (!Ignore) { |
2352 | 5 | Diag(ImplD->getLocation(), |
2353 | 5 | diag::warn_objc_implementation_missing_designated_init_override) |
2354 | 5 | << MD->getSelector(); |
2355 | 5 | Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here); |
2356 | 5 | } |
2357 | 7 | } |
2358 | 11 | } |
2359 | 11 | } |
2360 | | |
2361 | | /// AddPropertyAttrs - Propagates attributes from a property to the |
2362 | | /// implicitly-declared getter or setter for that property. |
2363 | | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
2364 | 468k | ObjCPropertyDecl *Property) { |
2365 | | // Should we just clone all attributes over? |
2366 | 468k | for (const auto *A : Property->attrs()) { |
2367 | 428k | if (isa<DeprecatedAttr>(A) || |
2368 | 428k | isa<UnavailableAttr>(A)428k || |
2369 | 428k | isa<AvailabilityAttr>(A)428k ) |
2370 | 424k | PropertyMethod->addAttr(A->clone(S.Context)); |
2371 | 428k | } |
2372 | 468k | } |
2373 | | |
2374 | | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
2375 | | /// have the property type and issue diagnostics if they don't. |
2376 | | /// Also synthesize a getter/setter method if none exist (and update the |
2377 | | /// appropriate lookup tables. |
2378 | 351k | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) { |
2379 | 351k | ObjCMethodDecl *GetterMethod, *SetterMethod; |
2380 | 351k | ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext()); |
2381 | 351k | if (CD->isInvalidDecl()) |
2382 | 2 | return; |
2383 | | |
2384 | 351k | bool IsClassProperty = property->isClassProperty(); |
2385 | 351k | GetterMethod = IsClassProperty ? |
2386 | 65.2k | CD->getClassMethod(property->getGetterName()) : |
2387 | 351k | CD->getInstanceMethod(property->getGetterName())286k ; |
2388 | | |
2389 | | // if setter or getter is not found in class extension, it might be |
2390 | | // in the primary class. |
2391 | 351k | if (!GetterMethod) |
2392 | 351k | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) |
2393 | 48.3k | if (CatDecl->IsClassExtension()) |
2394 | 583 | GetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
2395 | 1 | getClassMethod(property->getGetterName()) : |
2396 | 583 | CatDecl->getClassInterface()-> |
2397 | 582 | getInstanceMethod(property->getGetterName()); |
2398 | | |
2399 | 351k | SetterMethod = IsClassProperty ? |
2400 | 65.2k | CD->getClassMethod(property->getSetterName()) : |
2401 | 351k | CD->getInstanceMethod(property->getSetterName())286k ; |
2402 | 351k | if (!SetterMethod) |
2403 | 351k | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) |
2404 | 48.6k | if (CatDecl->IsClassExtension()) |
2405 | 583 | SetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
2406 | 1 | getClassMethod(property->getSetterName()) : |
2407 | 583 | CatDecl->getClassInterface()-> |
2408 | 582 | getInstanceMethod(property->getSetterName()); |
2409 | 351k | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
2410 | 351k | property->getLocation()); |
2411 | | |
2412 | | // synthesizing accessors must not result in a direct method that is not |
2413 | | // monomorphic |
2414 | 351k | if (!GetterMethod) { |
2415 | 351k | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) { |
2416 | 48.2k | auto *ExistingGetter = CatDecl->getClassInterface()->lookupMethod( |
2417 | 48.2k | property->getGetterName(), !IsClassProperty, true, false, CatDecl); |
2418 | 48.2k | if (ExistingGetter) { |
2419 | 131 | if (ExistingGetter->isDirectMethod() || property->isDirectProperty()125 ) { |
2420 | 9 | Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl) |
2421 | 9 | << property->isDirectProperty() << 1 /* property */ |
2422 | 9 | << ExistingGetter->isDirectMethod() |
2423 | 9 | << ExistingGetter->getDeclName(); |
2424 | 9 | Diag(ExistingGetter->getLocation(), diag::note_previous_declaration); |
2425 | 9 | } |
2426 | 131 | } |
2427 | 48.2k | } |
2428 | 351k | } |
2429 | | |
2430 | 351k | if (!property->isReadOnly() && !SetterMethod117k ) { |
2431 | 117k | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) { |
2432 | 6.11k | auto *ExistingSetter = CatDecl->getClassInterface()->lookupMethod( |
2433 | 6.11k | property->getSetterName(), !IsClassProperty, true, false, CatDecl); |
2434 | 6.11k | if (ExistingSetter) { |
2435 | 25 | if (ExistingSetter->isDirectMethod() || property->isDirectProperty()) { |
2436 | 0 | Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl) |
2437 | 0 | << property->isDirectProperty() << 1 /* property */ |
2438 | 0 | << ExistingSetter->isDirectMethod() |
2439 | 0 | << ExistingSetter->getDeclName(); |
2440 | 0 | Diag(ExistingSetter->getLocation(), diag::note_previous_declaration); |
2441 | 0 | } |
2442 | 25 | } |
2443 | 6.11k | } |
2444 | 117k | } |
2445 | | |
2446 | 351k | if (!property->isReadOnly() && SetterMethod117k ) { |
2447 | 100 | if (Context.getCanonicalType(SetterMethod->getReturnType()) != |
2448 | 100 | Context.VoidTy) |
2449 | 0 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
2450 | 100 | if (SetterMethod->param_size() != 1 || |
2451 | 100 | !Context.hasSameUnqualifiedType( |
2452 | 100 | (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
2453 | 100 | property->getType().getNonReferenceType())) { |
2454 | 0 | Diag(property->getLocation(), |
2455 | 0 | diag::warn_accessor_property_type_mismatch) |
2456 | 0 | << property->getDeclName() |
2457 | 0 | << SetterMethod->getSelector(); |
2458 | 0 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
2459 | 0 | } |
2460 | 100 | } |
2461 | | |
2462 | | // Synthesize getter/setter methods if none exist. |
2463 | | // Find the default getter and if one not found, add one. |
2464 | | // FIXME: The synthesized property we set here is misleading. We almost always |
2465 | | // synthesize these methods unless the user explicitly provided prototypes |
2466 | | // (which is odd, but allowed). Sema should be typechecking that the |
2467 | | // declarations jive in that situation (which it is not currently). |
2468 | 351k | if (!GetterMethod) { |
2469 | | // No instance/class method of same name as property getter name was found. |
2470 | | // Declare a getter method and add it to the list of methods |
2471 | | // for this class. |
2472 | 351k | SourceLocation Loc = property->getLocation(); |
2473 | | |
2474 | | // The getter returns the declared property type with all qualifiers |
2475 | | // removed. |
2476 | 351k | QualType resultTy = property->getType().getAtomicUnqualifiedType(); |
2477 | | |
2478 | | // If the property is null_resettable, the getter returns nonnull. |
2479 | 351k | if (property->getPropertyAttributes() & |
2480 | 351k | ObjCPropertyAttribute::kind_null_resettable) { |
2481 | 13.7k | QualType modifiedTy = resultTy; |
2482 | 13.7k | if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) { |
2483 | 13.7k | if (*nullability == NullabilityKind::Unspecified) |
2484 | 13.7k | resultTy = Context.getAttributedType(attr::TypeNonNull, |
2485 | 13.7k | modifiedTy, modifiedTy); |
2486 | 13.7k | } |
2487 | 13.7k | } |
2488 | | |
2489 | 351k | GetterMethod = ObjCMethodDecl::Create( |
2490 | 351k | Context, Loc, Loc, property->getGetterName(), resultTy, nullptr, CD, |
2491 | 351k | !IsClassProperty, /*isVariadic=*/false, |
2492 | 351k | /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, |
2493 | 351k | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
2494 | 351k | (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
2495 | 351k | ? ObjCMethodDecl::Optional1.05k |
2496 | 351k | : ObjCMethodDecl::Required350k ); |
2497 | 351k | CD->addDecl(GetterMethod); |
2498 | | |
2499 | 351k | AddPropertyAttrs(*this, GetterMethod, property); |
2500 | | |
2501 | 351k | if (property->isDirectProperty()) |
2502 | 44 | GetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc)); |
2503 | | |
2504 | 351k | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
2505 | 3 | GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context, |
2506 | 3 | Loc)); |
2507 | | |
2508 | 351k | if (property->hasAttr<ObjCReturnsInnerPointerAttr>()) |
2509 | 2.26k | GetterMethod->addAttr( |
2510 | 2.26k | ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc)); |
2511 | | |
2512 | 351k | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
2513 | 4 | GetterMethod->addAttr(SectionAttr::CreateImplicit( |
2514 | 4 | Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU, |
2515 | 4 | SectionAttr::GNU_section)); |
2516 | | |
2517 | 351k | if (getLangOpts().ObjCAutoRefCount) |
2518 | 10.3k | CheckARCMethodDecl(GetterMethod); |
2519 | 351k | } else |
2520 | | // A user declared getter will be synthesize when @synthesize of |
2521 | | // the property with the same name is seen in the @implementation |
2522 | 480 | GetterMethod->setPropertyAccessor(true); |
2523 | | |
2524 | 351k | GetterMethod->createImplicitParams(Context, |
2525 | 351k | GetterMethod->getClassInterface()); |
2526 | 351k | property->setGetterMethodDecl(GetterMethod); |
2527 | | |
2528 | | // Skip setter if property is read-only. |
2529 | 351k | if (!property->isReadOnly()) { |
2530 | | // Find the default setter and if one not found, add one. |
2531 | 117k | if (!SetterMethod) { |
2532 | | // No instance/class method of same name as property setter name was |
2533 | | // found. |
2534 | | // Declare a setter method and add it to the list of methods |
2535 | | // for this class. |
2536 | 117k | SourceLocation Loc = property->getLocation(); |
2537 | | |
2538 | 117k | SetterMethod = |
2539 | 117k | ObjCMethodDecl::Create(Context, Loc, Loc, |
2540 | 117k | property->getSetterName(), Context.VoidTy, |
2541 | 117k | nullptr, CD, !IsClassProperty, |
2542 | 117k | /*isVariadic=*/false, |
2543 | 117k | /*isPropertyAccessor=*/true, |
2544 | 117k | /*isSynthesizedAccessorStub=*/false, |
2545 | 117k | /*isImplicitlyDeclared=*/true, |
2546 | 117k | /*isDefined=*/false, |
2547 | 117k | (property->getPropertyImplementation() == |
2548 | 117k | ObjCPropertyDecl::Optional) ? |
2549 | 84 | ObjCMethodDecl::Optional : |
2550 | 117k | ObjCMethodDecl::Required117k ); |
2551 | | |
2552 | | // Remove all qualifiers from the setter's parameter type. |
2553 | 117k | QualType paramTy = |
2554 | 117k | property->getType().getUnqualifiedType().getAtomicUnqualifiedType(); |
2555 | | |
2556 | | // If the property is null_resettable, the setter accepts a |
2557 | | // nullable value. |
2558 | 117k | if (property->getPropertyAttributes() & |
2559 | 117k | ObjCPropertyAttribute::kind_null_resettable) { |
2560 | 13.7k | QualType modifiedTy = paramTy; |
2561 | 13.7k | if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){ |
2562 | 13.7k | if (*nullability == NullabilityKind::Unspecified) |
2563 | 13.7k | paramTy = Context.getAttributedType(attr::TypeNullable, |
2564 | 13.7k | modifiedTy, modifiedTy); |
2565 | 13.7k | } |
2566 | 13.7k | } |
2567 | | |
2568 | | // Invent the arguments for the setter. We don't bother making a |
2569 | | // nice name for the argument. |
2570 | 117k | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
2571 | 117k | Loc, Loc, |
2572 | 117k | property->getIdentifier(), |
2573 | 117k | paramTy, |
2574 | 117k | /*TInfo=*/nullptr, |
2575 | 117k | SC_None, |
2576 | 117k | nullptr); |
2577 | 117k | SetterMethod->setMethodParams(Context, Argument, None); |
2578 | | |
2579 | 117k | AddPropertyAttrs(*this, SetterMethod, property); |
2580 | | |
2581 | 117k | if (property->isDirectProperty()) |
2582 | 16 | SetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc)); |
2583 | | |
2584 | 117k | CD->addDecl(SetterMethod); |
2585 | 117k | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
2586 | 4 | SetterMethod->addAttr(SectionAttr::CreateImplicit( |
2587 | 4 | Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU, |
2588 | 4 | SectionAttr::GNU_section)); |
2589 | | // It's possible for the user to have set a very odd custom |
2590 | | // setter selector that causes it to have a method family. |
2591 | 117k | if (getLangOpts().ObjCAutoRefCount) |
2592 | 5.08k | CheckARCMethodDecl(SetterMethod); |
2593 | 117k | } else |
2594 | | // A user declared setter will be synthesize when @synthesize of |
2595 | | // the property with the same name is seen in the @implementation |
2596 | 100 | SetterMethod->setPropertyAccessor(true); |
2597 | | |
2598 | 117k | SetterMethod->createImplicitParams(Context, |
2599 | 117k | SetterMethod->getClassInterface()); |
2600 | 117k | property->setSetterMethodDecl(SetterMethod); |
2601 | 117k | } |
2602 | | // Add any synthesized methods to the global pool. This allows us to |
2603 | | // handle the following, which is supported by GCC (and part of the design). |
2604 | | // |
2605 | | // @interface Foo |
2606 | | // @property double bar; |
2607 | | // @end |
2608 | | // |
2609 | | // void thisIsUnfortunate() { |
2610 | | // id foo; |
2611 | | // double bar = [foo bar]; |
2612 | | // } |
2613 | | // |
2614 | 351k | if (!IsClassProperty) { |
2615 | 286k | if (GetterMethod) |
2616 | 286k | AddInstanceMethodToGlobalPool(GetterMethod); |
2617 | 286k | if (SetterMethod) |
2618 | 116k | AddInstanceMethodToGlobalPool(SetterMethod); |
2619 | 286k | } else { |
2620 | 65.2k | if (GetterMethod) |
2621 | 65.2k | AddFactoryMethodToGlobalPool(GetterMethod); |
2622 | 65.2k | if (SetterMethod) |
2623 | 1.26k | AddFactoryMethodToGlobalPool(SetterMethod); |
2624 | 65.2k | } |
2625 | | |
2626 | 351k | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); |
2627 | 351k | if (!CurrentClass) { |
2628 | 54.1k | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) |
2629 | 48.6k | CurrentClass = Cat->getClassInterface(); |
2630 | 5.53k | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) |
2631 | 0 | CurrentClass = Impl->getClassInterface(); |
2632 | 54.1k | } |
2633 | 351k | if (GetterMethod) |
2634 | 351k | CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); |
2635 | 351k | if (SetterMethod) |
2636 | 117k | CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); |
2637 | 351k | } |
2638 | | |
2639 | | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
2640 | | SourceLocation Loc, |
2641 | | unsigned &Attributes, |
2642 | 351k | bool propertyInPrimaryClass) { |
2643 | | // FIXME: Improve the reported location. |
2644 | 351k | if (!PDecl || PDecl->isInvalidDecl()) |
2645 | 7 | return; |
2646 | | |
2647 | 351k | if ((Attributes & ObjCPropertyAttribute::kind_readonly) && |
2648 | 351k | (Attributes & ObjCPropertyAttribute::kind_readwrite)234k ) |
2649 | 4 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2650 | 4 | << "readonly" << "readwrite"; |
2651 | | |
2652 | 351k | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
2653 | 351k | QualType PropertyTy = PropertyDecl->getType(); |
2654 | | |
2655 | | // Check for copy or retain on non-object types. |
2656 | 351k | if ((Attributes & |
2657 | 351k | (ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy | |
2658 | 351k | ObjCPropertyAttribute::kind_retain | |
2659 | 351k | ObjCPropertyAttribute::kind_strong)) && |
2660 | 351k | !PropertyTy->isObjCRetainableType()228k && |
2661 | 351k | !PropertyDecl->hasAttr<ObjCNSObjectAttr>()13 ) { |
2662 | 11 | Diag(Loc, diag::err_objc_property_requires_object) |
2663 | 11 | << (Attributes & ObjCPropertyAttribute::kind_weak |
2664 | 11 | ? "weak"1 |
2665 | 11 | : Attributes & ObjCPropertyAttribute::kind_copy10 |
2666 | 10 | ? "copy"3 |
2667 | 10 | : "retain (or strong)"7 ); |
2668 | 11 | Attributes &= |
2669 | 11 | ~(ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy | |
2670 | 11 | ObjCPropertyAttribute::kind_retain | |
2671 | 11 | ObjCPropertyAttribute::kind_strong); |
2672 | 11 | PropertyDecl->setInvalidDecl(); |
2673 | 11 | } |
2674 | | |
2675 | | // Check for assign on object types. |
2676 | 351k | if ((Attributes & ObjCPropertyAttribute::kind_assign) && |
2677 | 351k | !(Attributes & ObjCPropertyAttribute::kind_unsafe_unretained)6.03k && |
2678 | 351k | PropertyTy->isObjCRetainableType()6.02k && |
2679 | 351k | !PropertyTy->isObjCARCImplicitlyUnretainedType()4.25k ) { |
2680 | 4.23k | Diag(Loc, diag::warn_objc_property_assign_on_object); |
2681 | 4.23k | } |
2682 | | |
2683 | | // Check for more than one of { assign, copy, retain }. |
2684 | 351k | if (Attributes & ObjCPropertyAttribute::kind_assign) { |
2685 | 6.03k | if (Attributes & ObjCPropertyAttribute::kind_copy) { |
2686 | 4 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2687 | 4 | << "assign" << "copy"; |
2688 | 4 | Attributes &= ~ObjCPropertyAttribute::kind_copy; |
2689 | 4 | } |
2690 | 6.03k | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
2691 | 5 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2692 | 5 | << "assign" << "retain"; |
2693 | 5 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
2694 | 5 | } |
2695 | 6.03k | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
2696 | 2 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2697 | 2 | << "assign" << "strong"; |
2698 | 2 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
2699 | 2 | } |
2700 | 6.03k | if (getLangOpts().ObjCAutoRefCount && |
2701 | 6.03k | (Attributes & ObjCPropertyAttribute::kind_weak)456 ) { |
2702 | 2 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2703 | 2 | << "assign" << "weak"; |
2704 | 2 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
2705 | 2 | } |
2706 | 6.03k | if (PropertyDecl->hasAttr<IBOutletCollectionAttr>()) |
2707 | 2 | Diag(Loc, diag::warn_iboutletcollection_property_assign); |
2708 | 345k | } else if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) { |
2709 | 44 | if (Attributes & ObjCPropertyAttribute::kind_copy) { |
2710 | 3 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2711 | 3 | << "unsafe_unretained" << "copy"; |
2712 | 3 | Attributes &= ~ObjCPropertyAttribute::kind_copy; |
2713 | 3 | } |
2714 | 44 | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
2715 | 2 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2716 | 2 | << "unsafe_unretained" << "retain"; |
2717 | 2 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
2718 | 2 | } |
2719 | 44 | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
2720 | 2 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2721 | 2 | << "unsafe_unretained" << "strong"; |
2722 | 2 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
2723 | 2 | } |
2724 | 44 | if (getLangOpts().ObjCAutoRefCount && |
2725 | 44 | (Attributes & ObjCPropertyAttribute::kind_weak)31 ) { |
2726 | 0 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2727 | 0 | << "unsafe_unretained" << "weak"; |
2728 | 0 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
2729 | 0 | } |
2730 | 345k | } else if (Attributes & ObjCPropertyAttribute::kind_copy) { |
2731 | 195k | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
2732 | 4 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2733 | 4 | << "copy" << "retain"; |
2734 | 4 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
2735 | 4 | } |
2736 | 195k | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
2737 | 1 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2738 | 1 | << "copy" << "strong"; |
2739 | 1 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
2740 | 1 | } |
2741 | 195k | if (Attributes & ObjCPropertyAttribute::kind_weak) { |
2742 | 1 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2743 | 1 | << "copy" << "weak"; |
2744 | 1 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
2745 | 1 | } |
2746 | 195k | } else if (150k (Attributes & ObjCPropertyAttribute::kind_retain)150k && |
2747 | 150k | (Attributes & ObjCPropertyAttribute::kind_weak)20.0k ) { |
2748 | 3 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "retain" |
2749 | 3 | << "weak"; |
2750 | 3 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
2751 | 150k | } else if ((Attributes & ObjCPropertyAttribute::kind_strong) && |
2752 | 150k | (Attributes & ObjCPropertyAttribute::kind_weak)12.1k ) { |
2753 | 0 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "strong" |
2754 | 0 | << "weak"; |
2755 | 0 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
2756 | 0 | } |
2757 | | |
2758 | 351k | if (Attributes & ObjCPropertyAttribute::kind_weak) { |
2759 | | // 'weak' and 'nonnull' are mutually exclusive. |
2760 | 1.07k | if (auto nullability = PropertyTy->getNullability(Context)) { |
2761 | 910 | if (*nullability == NullabilityKind::NonNull) |
2762 | 3 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
2763 | 3 | << "nonnull" << "weak"; |
2764 | 910 | } |
2765 | 1.07k | } |
2766 | | |
2767 | 351k | if ((Attributes & ObjCPropertyAttribute::kind_atomic) && |
2768 | 351k | (Attributes & ObjCPropertyAttribute::kind_nonatomic)2.57k ) { |
2769 | 6 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "atomic" |
2770 | 6 | << "nonatomic"; |
2771 | 6 | Attributes &= ~ObjCPropertyAttribute::kind_atomic; |
2772 | 6 | } |
2773 | | |
2774 | | // Warn if user supplied no assignment attribute, property is |
2775 | | // readwrite, and this is an object type. |
2776 | 351k | if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()117k ) { |
2777 | 4.88k | if (Attributes & ObjCPropertyAttribute::kind_readonly) { |
2778 | | // do nothing |
2779 | 4.51k | } else if (368 getLangOpts().ObjCAutoRefCount368 ) { |
2780 | | // With arc, @property definitions should default to strong when |
2781 | | // not specified. |
2782 | 59 | PropertyDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
2783 | 309 | } else if (PropertyTy->isObjCObjectPointerType()) { |
2784 | 273 | bool isAnyClassTy = (PropertyTy->isObjCClassType() || |
2785 | 273 | PropertyTy->isObjCQualifiedClassType()265 ); |
2786 | | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
2787 | | // issue any warning. |
2788 | 273 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC10 ) |
2789 | 10 | ; |
2790 | 263 | else if (propertyInPrimaryClass) { |
2791 | | // Don't issue warning on property with no life time in class |
2792 | | // extension as it is inherited from property in primary class. |
2793 | | // Skip this warning in gc-only mode. |
2794 | 248 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
2795 | 248 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
2796 | | |
2797 | | // If non-gc code warn that this is likely inappropriate. |
2798 | 248 | if (getLangOpts().getGC() == LangOptions::NonGC) |
2799 | 239 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
2800 | 248 | } |
2801 | 273 | } |
2802 | | |
2803 | | // FIXME: Implement warning dependent on NSCopying being |
2804 | | // implemented. See also: |
2805 | | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
2806 | | // (please trim this list while you are at it). |
2807 | 4.88k | } |
2808 | | |
2809 | 351k | if (!(Attributes & ObjCPropertyAttribute::kind_copy) && |
2810 | 351k | !(Attributes & ObjCPropertyAttribute::kind_readonly)156k && |
2811 | 351k | getLangOpts().getGC() == LangOptions::GCOnly65.9k && |
2812 | 351k | PropertyTy->isBlockPointerType()4 ) |
2813 | 2 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
2814 | 351k | else if ((Attributes & ObjCPropertyAttribute::kind_retain) && |
2815 | 351k | !(Attributes & ObjCPropertyAttribute::kind_readonly)20.0k && |
2816 | 351k | !(Attributes & ObjCPropertyAttribute::kind_strong)7.92k && |
2817 | 351k | PropertyTy->isBlockPointerType()7.92k ) |
2818 | 3 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
2819 | | |
2820 | 351k | if ((Attributes & ObjCPropertyAttribute::kind_readonly) && |
2821 | 351k | (Attributes & ObjCPropertyAttribute::kind_setter)234k ) |
2822 | 1 | Diag(Loc, diag::warn_objc_readonly_property_has_setter); |
2823 | 351k | } |