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