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