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