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