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