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