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

source code of clang/lib/Sema/SemaObjCProperty.cpp