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 static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1444                                        ObjCContainerDecl::PropertyMap &PropMap,
1445                                        ObjCContainerDecl::PropertyMap &SuperPropMap,
1446                                        bool IncludeProtocols = true) {
1447 
1448   if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1449     for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1450          E = IDecl->prop_end(); P != E; ++P) {
1451       ObjCPropertyDecl *Prop = *P;
1452       PropMap[Prop->getIdentifier()] = Prop;
1453     }
1454     if (IncludeProtocols) {
1455       // Scan through class's protocols.
1456       for (ObjCInterfaceDecl::all_protocol_iterator
1457            PI = IDecl->all_referenced_protocol_begin(),
1458            E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1459           CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1460     }
1461   }
1462   if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1463     if (!CATDecl->IsClassExtension())
1464       for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1465            E = CATDecl->prop_end(); P != E; ++P) {
1466         ObjCPropertyDecl *Prop = *P;
1467         PropMap[Prop->getIdentifier()] = Prop;
1468       }
1469     if (IncludeProtocols) {
1470       // Scan through class's protocols.
1471       for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1472            E = CATDecl->protocol_end(); PI != E; ++PI)
1473         CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1474     }
1475   }
1476   else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1477     for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1478          E = PDecl->prop_end(); P != E; ++P) {
1479       ObjCPropertyDecl *Prop = *P;
1480       ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1481       // Exclude property for protocols which conform to class's super-class,
1482       // as super-class has to implement the property.
1483       if (!PropertyFromSuper ||
1484           PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
1485         ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1486         if (!PropEntry)
1487           PropEntry = Prop;
1488       }
1489     }
1490     // scan through protocol's protocols.
1491     for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1492          E = PDecl->protocol_end(); PI != E; ++PI)
1493       CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1494   }
1495 }
1496 
1497 /// CollectSuperClassPropertyImplementations - This routine collects list of
1498 /// properties to be implemented in super class(s) and also coming from their
1499 /// conforming protocols.
1500 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1501                                     ObjCInterfaceDecl::PropertyMap &PropMap) {
1502   if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1503     ObjCInterfaceDecl::PropertyDeclOrder PO;
1504     while (SDecl) {
1505       SDecl->collectPropertiesToImplement(PropMap, PO);
1506       SDecl = SDecl->getSuperClass();
1507     }
1508   }
1509 }
1510 
1511 /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1512 /// an ivar synthesized for 'Method' and 'Method' is a property accessor
1513 /// declared in class 'IFace'.
1514 bool
1515 Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1516                                      ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1517   if (!IV->getSynthesize())
1518     return false;
1519   ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1520                                             Method->isInstanceMethod());
1521   if (!IMD || !IMD->isPropertyAccessor())
1522     return false;
1523 
1524   // look up a property declaration whose one of its accessors is implemented
1525   // by this method.
1526   for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1527        E = IFace->prop_end(); P != E; ++P) {
1528     ObjCPropertyDecl *property = *P;
1529     if ((property->getGetterName() == IMD->getSelector() ||
1530          property->getSetterName() == IMD->getSelector()) &&
1531         (property->getPropertyIvarDecl() == IV))
1532       return true;
1533   }
1534   return false;
1535 }
1536 
1537 
1538 /// \brief Default synthesizes all properties which must be synthesized
1539 /// in class's \@implementation.
1540 void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1541                                        ObjCInterfaceDecl *IDecl) {
1542 
1543   ObjCInterfaceDecl::PropertyMap PropMap;
1544   ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1545   IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
1546   if (PropMap.empty())
1547     return;
1548   ObjCInterfaceDecl::PropertyMap SuperPropMap;
1549   CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1550 
1551   for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1552     ObjCPropertyDecl *Prop = PropertyOrder[i];
1553     // Is there a matching property synthesize/dynamic?
1554     if (Prop->isInvalidDecl() ||
1555         Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1556       continue;
1557     // Property may have been synthesized by user.
1558     if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1559       continue;
1560     if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1561       if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1562         continue;
1563       if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1564         continue;
1565     }
1566     // If property to be implemented in the super class, ignore.
1567     if (SuperPropMap[Prop->getIdentifier()]) {
1568       ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1569       if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1570           (PropInSuperClass->getPropertyAttributes() &
1571            ObjCPropertyDecl::OBJC_PR_readonly) &&
1572           !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1573           !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1574             Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1575               << Prop->getIdentifier();
1576             Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1577       }
1578       continue;
1579     }
1580     if (ObjCPropertyImplDecl *PID =
1581         IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1582       if (PID->getPropertyDecl() != Prop) {
1583         Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1584           << Prop->getIdentifier();
1585         if (!PID->getLocation().isInvalid())
1586           Diag(PID->getLocation(), diag::note_property_synthesize);
1587       }
1588       continue;
1589     }
1590     if (ObjCProtocolDecl *Proto =
1591           dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
1592       // We won't auto-synthesize properties declared in protocols.
1593       Diag(IMPDecl->getLocation(),
1594            diag::warn_auto_synthesizing_protocol_property)
1595         << Prop << Proto;
1596       Diag(Prop->getLocation(), diag::note_property_declare);
1597       continue;
1598     }
1599 
1600     // We use invalid SourceLocations for the synthesized ivars since they
1601     // aren't really synthesized at a particular location; they just exist.
1602     // Saying that they are located at the @implementation isn't really going
1603     // to help users.
1604     ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1605       ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1606                             true,
1607                             /* property = */ Prop->getIdentifier(),
1608                             /* ivar = */ Prop->getDefaultSynthIvarName(Context),
1609                             Prop->getLocation()));
1610     if (PIDecl) {
1611       Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
1612       Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1613     }
1614   }
1615 }
1616 
1617 void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1618   if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
1619     return;
1620   ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1621   if (!IC)
1622     return;
1623   if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1624     if (!IDecl->isObjCRequiresPropertyDefs())
1625       DefaultSynthesizeProperties(S, IC, IDecl);
1626 }
1627 
1628 static void DiagnoseUnimplementedAccessor(Sema &S,
1629                                           ObjCInterfaceDecl *PrimaryClass,
1630                                           Selector Method,
1631                                           ObjCImplDecl* IMPDecl,
1632                                           ObjCContainerDecl *CDecl,
1633                                           ObjCCategoryDecl *C,
1634                                           ObjCPropertyDecl *Prop,
1635                                           Sema::SelectorSet &SMap) {
1636   // When reporting on missing property setter/getter implementation in
1637   // categories, do not report when they are declared in primary class,
1638   // class's protocol, or one of it super classes. This is because,
1639   // the class is going to implement them.
1640   if (!SMap.count(Method) &&
1641       (PrimaryClass == 0 ||
1642        !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1643         S.Diag(IMPDecl->getLocation(),
1644                isa<ObjCCategoryDecl>(CDecl) ?
1645                diag::warn_setter_getter_impl_required_in_category :
1646                diag::warn_setter_getter_impl_required)
1647             << Prop->getDeclName() << Method;
1648         S.Diag(Prop->getLocation(),
1649              diag::note_property_declare);
1650         if (S.LangOpts.ObjCDefaultSynthProperties &&
1651             S.LangOpts.ObjCRuntime.isNonFragile())
1652           if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1653             if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1654             S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1655       }
1656 }
1657 
1658 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
1659                                            ObjCContainerDecl *CDecl,
1660                                            bool SynthesizeProperties) {
1661   ObjCContainerDecl::PropertyMap PropMap;
1662   ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1663 
1664   if (!SynthesizeProperties) {
1665     ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1666     // Gather properties which need not be implemented in this class
1667     // or category.
1668     if (!IDecl)
1669       if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1670         // For categories, no need to implement properties declared in
1671         // its primary class (and its super classes) if property is
1672         // declared in one of those containers.
1673         if ((IDecl = C->getClassInterface())) {
1674           ObjCInterfaceDecl::PropertyDeclOrder PO;
1675           IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1676         }
1677       }
1678     if (IDecl)
1679       CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1680 
1681     CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1682   }
1683 
1684   // Scan the @interface to see if any of the protocols it adopts
1685   // require an explicit implementation, via attribute
1686   // 'objc_protocol_requires_explicit_implementation'.
1687   if (IDecl) {
1688     OwningPtr<ObjCContainerDecl::PropertyMap> LazyMap;
1689 
1690     for (ObjCInterfaceDecl::all_protocol_iterator
1691           PI = IDecl->all_referenced_protocol_begin(),
1692           PE = IDecl->all_referenced_protocol_end();
1693           PI != PE; ++PI) {
1694       ObjCProtocolDecl *PDecl = *PI;
1695       if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1696         continue;
1697       // Lazily construct a set of all the properties in the @interface
1698       // of the class, without looking at the superclass.  We cannot
1699       // use the call to CollectImmediateProperties() above as that
1700       // utilizes information fromt he super class's properties as well
1701       // as scans the adopted protocols.  This work only triggers for protocols
1702       // with the attribute, which is very rare, and only occurs when
1703       // analyzing the @implementation.
1704       if (!LazyMap) {
1705         ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1706         LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1707         CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1708                                    /* IncludeProtocols */ false);
1709       }
1710       // Add the properties of 'PDecl' to the list of properties that
1711       // need to be implemented.
1712       for (ObjCProtocolDecl::prop_iterator
1713            PRI = PDecl->prop_begin(), PRE = PDecl->prop_end();
1714            PRI != PRE; ++PRI) {
1715         ObjCPropertyDecl *PropDecl = *PRI;
1716         if ((*LazyMap)[PRI->getIdentifier()])
1717           continue;
1718         PropMap[PRI->getIdentifier()] = PropDecl;
1719       }
1720     }
1721   }
1722 
1723   if (PropMap.empty())
1724     return;
1725 
1726   llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1727   for (ObjCImplDecl::propimpl_iterator
1728        I = IMPDecl->propimpl_begin(),
1729        EI = IMPDecl->propimpl_end(); I != EI; ++I)
1730     PropImplMap.insert(I->getPropertyDecl());
1731 
1732   SelectorSet InsMap;
1733   // Collect property accessors implemented in current implementation.
1734   for (ObjCImplementationDecl::instmeth_iterator
1735        I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1736     InsMap.insert((*I)->getSelector());
1737 
1738   ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1739   ObjCInterfaceDecl *PrimaryClass = 0;
1740   if (C && !C->IsClassExtension())
1741     if ((PrimaryClass = C->getClassInterface()))
1742       // Report unimplemented properties in the category as well.
1743       if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1744         // When reporting on missing setter/getters, do not report when
1745         // setter/getter is implemented in category's primary class
1746         // implementation.
1747         for (ObjCImplementationDecl::instmeth_iterator
1748              I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1749           InsMap.insert((*I)->getSelector());
1750       }
1751 
1752   for (ObjCContainerDecl::PropertyMap::iterator
1753        P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1754     ObjCPropertyDecl *Prop = P->second;
1755     // Is there a matching propery synthesize/dynamic?
1756     if (Prop->isInvalidDecl() ||
1757         Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1758         PropImplMap.count(Prop) ||
1759         Prop->getAvailability() == AR_Unavailable)
1760       continue;
1761 
1762     // Diagnose unimplemented getters and setters.
1763     DiagnoseUnimplementedAccessor(*this,
1764           PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1765     if (!Prop->isReadOnly())
1766       DiagnoseUnimplementedAccessor(*this,
1767                                     PrimaryClass, Prop->getSetterName(),
1768                                     IMPDecl, CDecl, C, Prop, InsMap);
1769   }
1770 }
1771 
1772 void
1773 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1774                                        ObjCContainerDecl* IDecl) {
1775   // Rules apply in non-GC mode only
1776   if (getLangOpts().getGC() != LangOptions::NonGC)
1777     return;
1778   for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1779        E = IDecl->prop_end();
1780        I != E; ++I) {
1781     ObjCPropertyDecl *Property = *I;
1782     ObjCMethodDecl *GetterMethod = 0;
1783     ObjCMethodDecl *SetterMethod = 0;
1784     bool LookedUpGetterSetter = false;
1785 
1786     unsigned Attributes = Property->getPropertyAttributes();
1787     unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
1788 
1789     if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1790         !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
1791       GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1792       SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1793       LookedUpGetterSetter = true;
1794       if (GetterMethod) {
1795         Diag(GetterMethod->getLocation(),
1796              diag::warn_default_atomic_custom_getter_setter)
1797           << Property->getIdentifier() << 0;
1798         Diag(Property->getLocation(), diag::note_property_declare);
1799       }
1800       if (SetterMethod) {
1801         Diag(SetterMethod->getLocation(),
1802              diag::warn_default_atomic_custom_getter_setter)
1803           << Property->getIdentifier() << 1;
1804         Diag(Property->getLocation(), diag::note_property_declare);
1805       }
1806     }
1807 
1808     // We only care about readwrite atomic property.
1809     if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1810         !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1811       continue;
1812     if (const ObjCPropertyImplDecl *PIDecl
1813          = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1814       if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1815         continue;
1816       if (!LookedUpGetterSetter) {
1817         GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1818         SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1819       }
1820       if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1821         SourceLocation MethodLoc =
1822           (GetterMethod ? GetterMethod->getLocation()
1823                         : SetterMethod->getLocation());
1824         Diag(MethodLoc, diag::warn_atomic_property_rule)
1825           << Property->getIdentifier() << (GetterMethod != 0)
1826           << (SetterMethod != 0);
1827         // fixit stuff.
1828         if (!AttributesAsWritten) {
1829           if (Property->getLParenLoc().isValid()) {
1830             // @property () ... case.
1831             SourceRange PropSourceRange(Property->getAtLoc(),
1832                                         Property->getLParenLoc());
1833             Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1834               FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1835           }
1836           else {
1837             //@property id etc.
1838             SourceLocation endLoc =
1839               Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1840             endLoc = endLoc.getLocWithOffset(-1);
1841             SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1842             Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1843               FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1844           }
1845         }
1846         else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1847           // @property () ... case.
1848           SourceLocation endLoc = Property->getLParenLoc();
1849           SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1850           Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1851            FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1852         }
1853         else
1854           Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
1855         Diag(Property->getLocation(), diag::note_property_declare);
1856       }
1857     }
1858   }
1859 }
1860 
1861 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1862   if (getLangOpts().getGC() == LangOptions::GCOnly)
1863     return;
1864 
1865   for (ObjCImplementationDecl::propimpl_iterator
1866          i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1867     ObjCPropertyImplDecl *PID = *i;
1868     const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1869     if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1870         !D->getInstanceMethod(PD->getGetterName())) {
1871       ObjCMethodDecl *method = PD->getGetterMethodDecl();
1872       if (!method)
1873         continue;
1874       ObjCMethodFamily family = method->getMethodFamily();
1875       if (family == OMF_alloc || family == OMF_copy ||
1876           family == OMF_mutableCopy || family == OMF_new) {
1877         if (getLangOpts().ObjCAutoRefCount)
1878           Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
1879         else
1880           Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
1881       }
1882     }
1883   }
1884 }
1885 
1886 void Sema::DiagnoseMissingDesignatedInitOverrides(
1887                                             const ObjCImplementationDecl *ImplD,
1888                                             const ObjCInterfaceDecl *IFD) {
1889   assert(IFD->hasDesignatedInitializers());
1890   const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1891   if (!SuperD)
1892     return;
1893 
1894   SelectorSet InitSelSet;
1895   for (ObjCImplementationDecl::instmeth_iterator
1896          I = ImplD->instmeth_begin(), E = ImplD->instmeth_end(); I!=E; ++I)
1897     if ((*I)->getMethodFamily() == OMF_init)
1898       InitSelSet.insert((*I)->getSelector());
1899 
1900   SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1901   SuperD->getDesignatedInitializers(DesignatedInits);
1902   for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1903          I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1904     const ObjCMethodDecl *MD = *I;
1905     if (!InitSelSet.count(MD->getSelector())) {
1906       Diag(ImplD->getLocation(),
1907            diag::warn_objc_implementation_missing_designated_init_override)
1908         << MD->getSelector();
1909       Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1910     }
1911   }
1912 }
1913 
1914 /// AddPropertyAttrs - Propagates attributes from a property to the
1915 /// implicitly-declared getter or setter for that property.
1916 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1917                              ObjCPropertyDecl *Property) {
1918   // Should we just clone all attributes over?
1919   for (Decl::attr_iterator A = Property->attr_begin(),
1920                         AEnd = Property->attr_end();
1921        A != AEnd; ++A) {
1922     if (isa<DeprecatedAttr>(*A) ||
1923         isa<UnavailableAttr>(*A) ||
1924         isa<AvailabilityAttr>(*A))
1925       PropertyMethod->addAttr((*A)->clone(S.Context));
1926   }
1927 }
1928 
1929 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1930 /// have the property type and issue diagnostics if they don't.
1931 /// Also synthesize a getter/setter method if none exist (and update the
1932 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1933 /// methods is the "right" thing to do.
1934 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1935                                ObjCContainerDecl *CD,
1936                                ObjCPropertyDecl *redeclaredProperty,
1937                                ObjCContainerDecl *lexicalDC) {
1938 
1939   ObjCMethodDecl *GetterMethod, *SetterMethod;
1940 
1941   GetterMethod = CD->getInstanceMethod(property->getGetterName());
1942   SetterMethod = CD->getInstanceMethod(property->getSetterName());
1943   DiagnosePropertyAccessorMismatch(property, GetterMethod,
1944                                    property->getLocation());
1945 
1946   if (SetterMethod) {
1947     ObjCPropertyDecl::PropertyAttributeKind CAttr =
1948       property->getPropertyAttributes();
1949     if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1950         Context.getCanonicalType(SetterMethod->getReturnType()) !=
1951             Context.VoidTy)
1952       Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1953     if (SetterMethod->param_size() != 1 ||
1954         !Context.hasSameUnqualifiedType(
1955           (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1956           property->getType().getNonReferenceType())) {
1957       Diag(property->getLocation(),
1958            diag::warn_accessor_property_type_mismatch)
1959         << property->getDeclName()
1960         << SetterMethod->getSelector();
1961       Diag(SetterMethod->getLocation(), diag::note_declared_at);
1962     }
1963   }
1964 
1965   // Synthesize getter/setter methods if none exist.
1966   // Find the default getter and if one not found, add one.
1967   // FIXME: The synthesized property we set here is misleading. We almost always
1968   // synthesize these methods unless the user explicitly provided prototypes
1969   // (which is odd, but allowed). Sema should be typechecking that the
1970   // declarations jive in that situation (which it is not currently).
1971   if (!GetterMethod) {
1972     // No instance method of same name as property getter name was found.
1973     // Declare a getter 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     GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1980                              property->getGetterName(),
1981                              property->getType(), 0, CD, /*isInstance=*/true,
1982                              /*isVariadic=*/false, /*isPropertyAccessor=*/true,
1983                              /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1984                              (property->getPropertyImplementation() ==
1985                               ObjCPropertyDecl::Optional) ?
1986                              ObjCMethodDecl::Optional :
1987                              ObjCMethodDecl::Required);
1988     CD->addDecl(GetterMethod);
1989 
1990     AddPropertyAttrs(*this, GetterMethod, property);
1991 
1992     // FIXME: Eventually this shouldn't be needed, as the lexical context
1993     // and the real context should be the same.
1994     if (lexicalDC)
1995       GetterMethod->setLexicalDeclContext(lexicalDC);
1996     if (property->hasAttr<NSReturnsNotRetainedAttr>())
1997       GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1998                                                                      Loc));
1999 
2000     if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2001       GetterMethod->addAttr(
2002         ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
2003 
2004     if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2005       GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
2006                                                         Loc));
2007 
2008     if (getLangOpts().ObjCAutoRefCount)
2009       CheckARCMethodDecl(GetterMethod);
2010   } else
2011     // A user declared getter will be synthesize when @synthesize of
2012     // the property with the same name is seen in the @implementation
2013     GetterMethod->setPropertyAccessor(true);
2014   property->setGetterMethodDecl(GetterMethod);
2015 
2016   // Skip setter if property is read-only.
2017   if (!property->isReadOnly()) {
2018     // Find the default setter and if one not found, add one.
2019     if (!SetterMethod) {
2020       // No instance method of same name as property setter name was found.
2021       // Declare a setter method and add it to the list of methods
2022       // for this class.
2023       SourceLocation Loc = redeclaredProperty ?
2024         redeclaredProperty->getLocation() :
2025         property->getLocation();
2026 
2027       SetterMethod =
2028         ObjCMethodDecl::Create(Context, Loc, Loc,
2029                                property->getSetterName(), Context.VoidTy, 0,
2030                                CD, /*isInstance=*/true, /*isVariadic=*/false,
2031                                /*isPropertyAccessor=*/true,
2032                                /*isImplicitlyDeclared=*/true,
2033                                /*isDefined=*/false,
2034                                (property->getPropertyImplementation() ==
2035                                 ObjCPropertyDecl::Optional) ?
2036                                 ObjCMethodDecl::Optional :
2037                                 ObjCMethodDecl::Required);
2038 
2039       // Invent the arguments for the setter. We don't bother making a
2040       // nice name for the argument.
2041       ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2042                                                   Loc, Loc,
2043                                                   property->getIdentifier(),
2044                                     property->getType().getUnqualifiedType(),
2045                                                   /*TInfo=*/0,
2046                                                   SC_None,
2047                                                   0);
2048       SetterMethod->setMethodParams(Context, Argument, None);
2049 
2050       AddPropertyAttrs(*this, SetterMethod, property);
2051 
2052       CD->addDecl(SetterMethod);
2053       // FIXME: Eventually this shouldn't be needed, as the lexical context
2054       // and the real context should be the same.
2055       if (lexicalDC)
2056         SetterMethod->setLexicalDeclContext(lexicalDC);
2057       if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2058         SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2059                                                           SA->getName(), Loc));
2060       // It's possible for the user to have set a very odd custom
2061       // setter selector that causes it to have a method family.
2062       if (getLangOpts().ObjCAutoRefCount)
2063         CheckARCMethodDecl(SetterMethod);
2064     } else
2065       // A user declared setter will be synthesize when @synthesize of
2066       // the property with the same name is seen in the @implementation
2067       SetterMethod->setPropertyAccessor(true);
2068     property->setSetterMethodDecl(SetterMethod);
2069   }
2070   // Add any synthesized methods to the global pool. This allows us to
2071   // handle the following, which is supported by GCC (and part of the design).
2072   //
2073   // @interface Foo
2074   // @property double bar;
2075   // @end
2076   //
2077   // void thisIsUnfortunate() {
2078   //   id foo;
2079   //   double bar = [foo bar];
2080   // }
2081   //
2082   if (GetterMethod)
2083     AddInstanceMethodToGlobalPool(GetterMethod);
2084   if (SetterMethod)
2085     AddInstanceMethodToGlobalPool(SetterMethod);
2086 
2087   ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2088   if (!CurrentClass) {
2089     if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2090       CurrentClass = Cat->getClassInterface();
2091     else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2092       CurrentClass = Impl->getClassInterface();
2093   }
2094   if (GetterMethod)
2095     CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2096   if (SetterMethod)
2097     CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
2098 }
2099 
2100 void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
2101                                        SourceLocation Loc,
2102                                        unsigned &Attributes,
2103                                        bool propertyInPrimaryClass) {
2104   // FIXME: Improve the reported location.
2105   if (!PDecl || PDecl->isInvalidDecl())
2106     return;
2107 
2108   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2109       (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2110     Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2111     << "readonly" << "readwrite";
2112 
2113   ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2114   QualType PropertyTy = PropertyDecl->getType();
2115   unsigned PropertyOwnership = getOwnershipRule(Attributes);
2116 
2117   // 'readonly' property with no obvious lifetime.
2118   // its life time will be determined by its backing ivar.
2119   if (getLangOpts().ObjCAutoRefCount &&
2120       Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2121       PropertyTy->isObjCRetainableType() &&
2122       !PropertyOwnership)
2123     return;
2124 
2125   // Check for copy or retain on non-object types.
2126   if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2127                     ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2128       !PropertyTy->isObjCRetainableType() &&
2129       !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
2130     Diag(Loc, diag::err_objc_property_requires_object)
2131       << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2132           Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2133     Attributes &= ~(ObjCDeclSpec::DQ_PR_weak   | ObjCDeclSpec::DQ_PR_copy |
2134                     ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
2135     PropertyDecl->setInvalidDecl();
2136   }
2137 
2138   // Check for more than one of { assign, copy, retain }.
2139   if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2140     if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2141       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2142         << "assign" << "copy";
2143       Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2144     }
2145     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2146       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2147         << "assign" << "retain";
2148       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2149     }
2150     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2151       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2152         << "assign" << "strong";
2153       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2154     }
2155     if (getLangOpts().ObjCAutoRefCount  &&
2156         (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2157       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2158         << "assign" << "weak";
2159       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2160     }
2161     if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
2162       Diag(Loc, diag::warn_iboutletcollection_property_assign);
2163   } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2164     if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2165       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2166         << "unsafe_unretained" << "copy";
2167       Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2168     }
2169     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2170       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2171         << "unsafe_unretained" << "retain";
2172       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2173     }
2174     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2175       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2176         << "unsafe_unretained" << "strong";
2177       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2178     }
2179     if (getLangOpts().ObjCAutoRefCount  &&
2180         (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2181       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2182         << "unsafe_unretained" << "weak";
2183       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2184     }
2185   } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2186     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2187       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2188         << "copy" << "retain";
2189       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2190     }
2191     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2192       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2193         << "copy" << "strong";
2194       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2195     }
2196     if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2197       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2198         << "copy" << "weak";
2199       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2200     }
2201   }
2202   else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2203            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2204       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2205         << "retain" << "weak";
2206       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2207   }
2208   else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2209            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2210       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2211         << "strong" << "weak";
2212       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2213   }
2214 
2215   if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2216       (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2217       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2218         << "atomic" << "nonatomic";
2219       Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2220   }
2221 
2222   // Warn if user supplied no assignment attribute, property is
2223   // readwrite, and this is an object type.
2224   if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2225                       ObjCDeclSpec::DQ_PR_unsafe_unretained |
2226                       ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2227                       ObjCDeclSpec::DQ_PR_weak)) &&
2228       PropertyTy->isObjCObjectPointerType()) {
2229       if (getLangOpts().ObjCAutoRefCount)
2230         // With arc,  @property definitions should default to (strong) when
2231         // not specified; including when property is 'readonly'.
2232         PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2233       else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
2234         bool isAnyClassTy =
2235           (PropertyTy->isObjCClassType() ||
2236            PropertyTy->isObjCQualifiedClassType());
2237         // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2238         // issue any warning.
2239         if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
2240           ;
2241         else if (propertyInPrimaryClass) {
2242           // Don't issue warning on property with no life time in class
2243           // extension as it is inherited from property in primary class.
2244           // Skip this warning in gc-only mode.
2245           if (getLangOpts().getGC() != LangOptions::GCOnly)
2246             Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2247 
2248           // If non-gc code warn that this is likely inappropriate.
2249           if (getLangOpts().getGC() == LangOptions::NonGC)
2250             Diag(Loc, diag::warn_objc_property_default_assign_on_object);
2251         }
2252       }
2253 
2254     // FIXME: Implement warning dependent on NSCopying being
2255     // implemented. See also:
2256     // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2257     // (please trim this list while you are at it).
2258   }
2259 
2260   if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2261       &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
2262       && getLangOpts().getGC() == LangOptions::GCOnly
2263       && PropertyTy->isBlockPointerType())
2264     Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
2265   else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2266            !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2267            !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2268            PropertyTy->isBlockPointerType())
2269       Diag(Loc, diag::warn_objc_property_retain_of_block);
2270 
2271   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2272       (Attributes & ObjCDeclSpec::DQ_PR_setter))
2273     Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2274 
2275 }
2276