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