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