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