SemaObjCProperty.cpp revision 744147138cd2a77320c431578da130cc3d81a1d5
19d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
29d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//
39d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//                     The LLVM Compiler Infrastructure
49d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//
59d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek// This file is distributed under the University of Illinois Open Source
69d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek// License. See LICENSE.TXT for details.
79d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//
89d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//===----------------------------------------------------------------------===//
99d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//
109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//  This file implements semantic analysis for Objective C @property and
119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//  @synthesize declarations.
129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//
139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek//===----------------------------------------------------------------------===//
149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
177cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h"
1817cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian#include "clang/AST/ExprObjC.h"
1957e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian#include "clang/AST/ExprCXX.h"
20c80553efdc269dabd4ed0c3210555f45729ab5ddArgyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
2150df6ae41f232612e5e88b19e0db9900d08d2f6cJohn McCall#include "llvm/ADT/DenseSet.h"
228fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer#include "llvm/ADT/SmallString.h"
239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekusing namespace clang;
259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
2628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek//===----------------------------------------------------------------------===//
2728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek// Grammar actions.
2828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek//===----------------------------------------------------------------------===//
2928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
30265941bc308d65cc270d5c4de5806f37ce405606John McCall/// getImpliedARCOwnership - Given a set of property attributes and a
31265941bc308d65cc270d5c4de5806f37ce405606John McCall/// type, infer an expected lifetime.  The type's ownership qualification
32265941bc308d65cc270d5c4de5806f37ce405606John McCall/// is not considered.
33265941bc308d65cc270d5c4de5806f37ce405606John McCall///
34265941bc308d65cc270d5c4de5806f37ce405606John McCall/// Returns OCL_None if the attributes as stated do not imply an ownership.
35265941bc308d65cc270d5c4de5806f37ce405606John McCall/// Never returns OCL_Autoreleasing.
36265941bc308d65cc270d5c4de5806f37ce405606John McCallstatic Qualifiers::ObjCLifetime getImpliedARCOwnership(
37265941bc308d65cc270d5c4de5806f37ce405606John McCall                               ObjCPropertyDecl::PropertyAttributeKind attrs,
38265941bc308d65cc270d5c4de5806f37ce405606John McCall                                                QualType type) {
39265941bc308d65cc270d5c4de5806f37ce405606John McCall  // retain, strong, copy, weak, and unsafe_unretained are only legal
40265941bc308d65cc270d5c4de5806f37ce405606John McCall  // on properties of retainable pointer type.
41265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
42265941bc308d65cc270d5c4de5806f37ce405606John McCall               ObjCPropertyDecl::OBJC_PR_strong |
43265941bc308d65cc270d5c4de5806f37ce405606John McCall               ObjCPropertyDecl::OBJC_PR_copy)) {
445fa065b55bd622f20656e0de3b1f122951146784Fariborz Jahanian    return type->getObjCARCImplicitLifetime();
45265941bc308d65cc270d5c4de5806f37ce405606John McCall  } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
46265941bc308d65cc270d5c4de5806f37ce405606John McCall    return Qualifiers::OCL_Weak;
47265941bc308d65cc270d5c4de5806f37ce405606John McCall  } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
48265941bc308d65cc270d5c4de5806f37ce405606John McCall    return Qualifiers::OCL_ExplicitNone;
49265941bc308d65cc270d5c4de5806f37ce405606John McCall  }
50265941bc308d65cc270d5c4de5806f37ce405606John McCall
51265941bc308d65cc270d5c4de5806f37ce405606John McCall  // assign can appear on other types, so we have to check the
52265941bc308d65cc270d5c4de5806f37ce405606John McCall  // property type.
53265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
54265941bc308d65cc270d5c4de5806f37ce405606John McCall      type->isObjCRetainableType()) {
55265941bc308d65cc270d5c4de5806f37ce405606John McCall    return Qualifiers::OCL_ExplicitNone;
56265941bc308d65cc270d5c4de5806f37ce405606John McCall  }
57265941bc308d65cc270d5c4de5806f37ce405606John McCall
58265941bc308d65cc270d5c4de5806f37ce405606John McCall  return Qualifiers::OCL_None;
59265941bc308d65cc270d5c4de5806f37ce405606John McCall}
60265941bc308d65cc270d5c4de5806f37ce405606John McCall
61f85e193739c953358c865005855253af4f68a497John McCall/// Check the internal consistency of a property declaration.
62f85e193739c953358c865005855253af4f68a497John McCallstatic void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
63f85e193739c953358c865005855253af4f68a497John McCall  if (property->isInvalidDecl()) return;
64f85e193739c953358c865005855253af4f68a497John McCall
65f85e193739c953358c865005855253af4f68a497John McCall  ObjCPropertyDecl::PropertyAttributeKind propertyKind
66f85e193739c953358c865005855253af4f68a497John McCall    = property->getPropertyAttributes();
67f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime propertyLifetime
68f85e193739c953358c865005855253af4f68a497John McCall    = property->getType().getObjCLifetime();
69f85e193739c953358c865005855253af4f68a497John McCall
70f85e193739c953358c865005855253af4f68a497John McCall  // Nothing to do if we don't have a lifetime.
71f85e193739c953358c865005855253af4f68a497John McCall  if (propertyLifetime == Qualifiers::OCL_None) return;
72f85e193739c953358c865005855253af4f68a497John McCall
73265941bc308d65cc270d5c4de5806f37ce405606John McCall  Qualifiers::ObjCLifetime expectedLifetime
74265941bc308d65cc270d5c4de5806f37ce405606John McCall    = getImpliedARCOwnership(propertyKind, property->getType());
75265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (!expectedLifetime) {
76f85e193739c953358c865005855253af4f68a497John McCall    // We have a lifetime qualifier but no dominating property
77265941bc308d65cc270d5c4de5806f37ce405606John McCall    // attribute.  That's okay, but restore reasonable invariants by
78265941bc308d65cc270d5c4de5806f37ce405606John McCall    // setting the property attribute according to the lifetime
79265941bc308d65cc270d5c4de5806f37ce405606John McCall    // qualifier.
80265941bc308d65cc270d5c4de5806f37ce405606John McCall    ObjCPropertyDecl::PropertyAttributeKind attr;
81265941bc308d65cc270d5c4de5806f37ce405606John McCall    if (propertyLifetime == Qualifiers::OCL_Strong) {
82265941bc308d65cc270d5c4de5806f37ce405606John McCall      attr = ObjCPropertyDecl::OBJC_PR_strong;
83265941bc308d65cc270d5c4de5806f37ce405606John McCall    } else if (propertyLifetime == Qualifiers::OCL_Weak) {
84265941bc308d65cc270d5c4de5806f37ce405606John McCall      attr = ObjCPropertyDecl::OBJC_PR_weak;
85265941bc308d65cc270d5c4de5806f37ce405606John McCall    } else {
86265941bc308d65cc270d5c4de5806f37ce405606John McCall      assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
87265941bc308d65cc270d5c4de5806f37ce405606John McCall      attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
88265941bc308d65cc270d5c4de5806f37ce405606John McCall    }
89265941bc308d65cc270d5c4de5806f37ce405606John McCall    property->setPropertyAttributes(attr);
90f85e193739c953358c865005855253af4f68a497John McCall    return;
91f85e193739c953358c865005855253af4f68a497John McCall  }
92f85e193739c953358c865005855253af4f68a497John McCall
93f85e193739c953358c865005855253af4f68a497John McCall  if (propertyLifetime == expectedLifetime) return;
94f85e193739c953358c865005855253af4f68a497John McCall
95f85e193739c953358c865005855253af4f68a497John McCall  property->setInvalidDecl();
96f85e193739c953358c865005855253af4f68a497John McCall  S.Diag(property->getLocation(),
97b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis         diag::err_arc_inconsistent_property_ownership)
98f85e193739c953358c865005855253af4f68a497John McCall    << property->getDeclName()
99265941bc308d65cc270d5c4de5806f37ce405606John McCall    << expectedLifetime
100f85e193739c953358c865005855253af4f68a497John McCall    << propertyLifetime;
101f85e193739c953358c865005855253af4f68a497John McCall}
102f85e193739c953358c865005855253af4f68a497John McCall
103d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
10477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                          SourceLocation LParenLoc,
105d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          FieldDeclarator &FD,
106d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          ObjCDeclSpec &ODS,
107d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          Selector GetterSel,
108d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          Selector SetterSel,
109d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          bool *isOverridingProperty,
1104a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek                          tok::ObjCKeywordKind MethodImplKind,
1114a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek                          DeclContext *lexicalDC) {
11228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  unsigned Attributes = ODS.getPropertyAttributes();
113f85e193739c953358c865005855253af4f68a497John McCall  TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
114f85e193739c953358c865005855253af4f68a497John McCall  QualType T = TSI->getType();
1154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if ((getLangOpts().getGC() != LangOptions::NonGC &&
116f85e193739c953358c865005855253af4f68a497John McCall       T.isObjCGCWeak()) ||
1174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      (getLangOpts().ObjCAutoRefCount &&
118f85e193739c953358c865005855253af4f68a497John McCall       T.getObjCLifetime() == Qualifiers::OCL_Weak))
119f85e193739c953358c865005855253af4f68a497John McCall    Attributes |= ObjCDeclSpec::DQ_PR_weak;
120f85e193739c953358c865005855253af4f68a497John McCall
12128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
12228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                      // default is readwrite!
12328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                      !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
12428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // property is defaulted to 'assign' if it is readwrite and is
12528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // not retain or copy
12628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
12728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                   (isReadWrite &&
12828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                    !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
129f85e193739c953358c865005855253af4f68a497John McCall                    !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
130f85e193739c953358c865005855253af4f68a497John McCall                    !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
131f85e193739c953358c865005855253af4f68a497John McCall                    !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
132f85e193739c953358c865005855253af4f68a497John McCall                    !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
13314086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian
134e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // Proceed with constructing the ObjCPropertDecls.
135a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
136e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
13728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
138ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian    if (CDecl->IsClassExtension()) {
13977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
140ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian                                           FD, GetterSel, SetterSel,
141ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian                                           isAssign, isReadWrite,
142ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian                                           Attributes,
143dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                           ODS.getPropertyAttributes(),
144ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian                                           isOverridingProperty, TSI,
145ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian                                           MethodImplKind);
146f85e193739c953358c865005855253af4f68a497John McCall      if (Res) {
147ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian        CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
1484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().ObjCAutoRefCount)
149f85e193739c953358c865005855253af4f68a497John McCall          checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
150f85e193739c953358c865005855253af4f68a497John McCall      }
151ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian      return Res;
152ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian    }
153ae415dc21ee6402cee1675ec8bb965a24f9e5b6bFariborz Jahanian
15477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian  ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
155f85e193739c953358c865005855253af4f68a497John McCall                                             GetterSel, SetterSel,
156f85e193739c953358c865005855253af4f68a497John McCall                                             isAssign, isReadWrite,
157dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                             Attributes,
158dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                             ODS.getPropertyAttributes(),
159dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                             TSI, MethodImplKind);
1604a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek  if (lexicalDC)
1614a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek    Res->setLexicalDeclContext(lexicalDC);
1624a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek
163842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian  // Validate the attributes on the @property.
164842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian  CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
165f85e193739c953358c865005855253af4f68a497John McCall
1664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount)
167f85e193739c953358c865005855253af4f68a497John McCall    checkARCPropertyDecl(*this, Res);
168f85e193739c953358c865005855253af4f68a497John McCall
169842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian  return Res;
170e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek}
171e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
172b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidisstatic ObjCPropertyDecl::PropertyAttributeKind
173b98ffded10453d80369951f33f3892f35d747c95Argyrios KyrtzidismakePropertyAttributesAsWritten(unsigned Attributes) {
174b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  unsigned attributesAsWritten = 0;
175b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
177b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
179b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_getter)
180b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
181b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_setter)
182b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
183b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_assign)
184b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
185b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_retain)
186b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
187b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_strong)
188b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
189b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_weak)
190b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
191b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_copy)
192b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
193b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
194b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
195b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
196b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
197b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
198b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis    attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
199b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis
200b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
201b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis}
202b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis
203d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *
204a28948f34817476d02412fa204cae038e228c827Fariborz JahanianSema::HandlePropertyInClassExtension(Scope *S,
20577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                     SourceLocation AtLoc,
20677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                     SourceLocation LParenLoc,
20777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                     FieldDeclarator &FD,
208e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     Selector GetterSel, Selector SetterSel,
209e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     const bool isAssign,
210e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     const bool isReadWrite,
211e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     const unsigned Attributes,
212dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                     const unsigned AttributesAsWritten,
213e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     bool *isOverridingProperty,
21483a230c83a54190366138c1a4f4310ef838b88fcJohn McCall                                     TypeSourceInfo *T,
215e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                     tok::ObjCKeywordKind MethodImplKind) {
21658a764940df0cd41767367eb2f4fced6f374176bFariborz Jahanian  ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
217e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // Diagnose if this property is already in continuation class.
218a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  DeclContext *DC = CurContext;
219e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  IdentifierInfo *PropertyId = FD.D.getIdentifier();
2202a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian  ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
2212a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian
2222a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian  if (CCPrimary)
2232a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian    // Check for duplicate declaration of this property in current and
2242a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian    // other class extensions.
2252a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian    for (const ObjCCategoryDecl *ClsExtDecl =
2262a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian         CCPrimary->getFirstClassExtension();
2272a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian         ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2282a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian      if (ObjCPropertyDecl *prevDecl =
2292a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian          ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
2302a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian        Diag(AtLoc, diag::err_duplicate_property);
2312a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian        Diag(prevDecl->getLocation(), diag::note_property_declare);
2322a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian        return 0;
2332a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian      }
2342a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian    }
2352a2891410b02d146b779cdb29eeb9b6d1a9fa030Fariborz Jahanian
236e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // Create a new ObjCPropertyDecl with the DeclContext being
237e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // the class extension.
23888f5e9be350f4b107f8665183a6d441874e0fcc7Fariborz Jahanian  // FIXME. We should really be using CreatePropertyDecl for this.
239e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  ObjCPropertyDecl *PDecl =
240e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
24177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                             PropertyId, AtLoc, LParenLoc, T);
242b98ffded10453d80369951f33f3892f35d747c95Argyrios Kyrtzidis  PDecl->setPropertyAttributesAsWritten(
243dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                          makePropertyAttributesAsWritten(AttributesAsWritten));
24422f757b38da3fc9f17ea9e99524064fdfbca3456Fariborz Jahanian  if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
24522f757b38da3fc9f17ea9e99524064fdfbca3456Fariborz Jahanian    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
24622f757b38da3fc9f17ea9e99524064fdfbca3456Fariborz Jahanian  if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
24722f757b38da3fc9f17ea9e99524064fdfbca3456Fariborz Jahanian    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
24888f5e9be350f4b107f8665183a6d441874e0fcc7Fariborz Jahanian  // Set setter/getter selector name. Needed later.
24988f5e9be350f4b107f8665183a6d441874e0fcc7Fariborz Jahanian  PDecl->setGetterName(GetterSel);
25088f5e9be350f4b107f8665183a6d441874e0fcc7Fariborz Jahanian  PDecl->setSetterName(SetterSel);
25191ae6b43b3a294243698cdb544dd7a18efa4c7a8Douglas Gregor  ProcessDeclAttributes(S, PDecl, FD.D);
252e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  DC->addDecl(PDecl);
253e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
254e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // We need to look in the @interface to see if the @property was
255e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // already declared.
256e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  if (!CCPrimary) {
257e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    Diag(CDecl->getLocation(), diag::err_continuation_class);
258e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    *isOverridingProperty = true;
259d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
260e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  }
261e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
262e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // Find the property in continuation class's primary class only.
263e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  ObjCPropertyDecl *PIDecl =
264e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
265e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
266e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  if (!PIDecl) {
267e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    // No matching property found in the primary class. Just fall thru
268e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    // and add property to continuation class's primary class.
269d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis    ObjCPropertyDecl *PrimaryPDecl =
27077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
271e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                         FD, GetterSel, SetterSel, isAssign, isReadWrite,
272dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                         Attributes,AttributesAsWritten, T, MethodImplKind, DC);
273e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
274e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    // A case of continuation class adding a new property in the class. This
275e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    // is not what it was meant for. However, gcc supports it and so should we.
276e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    // Make sure setter/getters are declared here.
277d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis    ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
278a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek                        /* lexicalDC = */ CDecl);
279d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis    PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
280d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis    PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
281c80553efdc269dabd4ed0c3210555f45729ab5ddArgyrios Kyrtzidis    if (ASTMutationListener *L = Context.getASTMutationListener())
282d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis      L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
283d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis    return PrimaryPDecl;
284e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  }
285e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian  if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
286e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian    bool IncompatibleObjC = false;
287e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian    QualType ConvertedType;
288ff2a0ecae1d7692dbb63aa540dba41a8c21d7ee4Fariborz Jahanian    // Relax the strict type matching for property type in continuation class.
289ff2a0ecae1d7692dbb63aa540dba41a8c21d7ee4Fariborz Jahanian    // Allow property object type of continuation class to be different as long
290ad7eff2faf517779689327dc268817c2c2c8ebc4Fariborz Jahanian    // as it narrows the object type in its primary class property. Note that
291ad7eff2faf517779689327dc268817c2c2c8ebc4Fariborz Jahanian    // this conversion is safe only because the wider type is for a 'readonly'
292ad7eff2faf517779689327dc268817c2c2c8ebc4Fariborz Jahanian    // property in primary class and 'narrowed' type for a 'readwrite' property
293ad7eff2faf517779689327dc268817c2c2c8ebc4Fariborz Jahanian    // in continuation class.
294e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian    if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
295e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian        !isa<ObjCObjectPointerType>(PDecl->getType()) ||
296e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian        (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
297e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian                                  ConvertedType, IncompatibleObjC))
298e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian        || IncompatibleObjC) {
299e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian      Diag(AtLoc,
300e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian          diag::err_type_mismatch_continuation_class) << PDecl->getType();
301e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian      Diag(PIDecl->getLocation(), diag::note_property_declare);
302e23518342e326e3d6e9139f05384f030e2a94d4aFariborz Jahanian    }
303a4b984d8396724a8e1137c22186c558c0cb0bf3fFariborz Jahanian  }
304a4b984d8396724a8e1137c22186c558c0cb0bf3fFariborz Jahanian
305e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // The property 'PIDecl's readonly attribute will be over-ridden
306e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // with continuation class's readwrite property attribute!
30780aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
308e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
309e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    unsigned retainCopyNonatomic =
310e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    (ObjCPropertyDecl::OBJC_PR_retain |
311f85e193739c953358c865005855253af4f68a497John McCall     ObjCPropertyDecl::OBJC_PR_strong |
312e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek     ObjCPropertyDecl::OBJC_PR_copy |
313e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek     ObjCPropertyDecl::OBJC_PR_nonatomic);
314e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    if ((Attributes & retainCopyNonatomic) !=
315e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek        (PIkind & retainCopyNonatomic)) {
316e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      Diag(AtLoc, diag::warn_property_attr_mismatch);
317e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      Diag(PIDecl->getLocation(), diag::note_property_declare);
318e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    }
3199944c769b69b1904a7b16d3ce10fbdc9c67c764fTed Kremenek    DeclContext *DC = cast<DeclContext>(CCPrimary);
3209944c769b69b1904a7b16d3ce10fbdc9c67c764fTed Kremenek    if (!ObjCPropertyDecl::findPropertyDecl(DC,
3219944c769b69b1904a7b16d3ce10fbdc9c67c764fTed Kremenek                                 PIDecl->getDeclName().getAsIdentifierInfo())) {
322e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      // Protocol is not in the primary class. Must build one for it.
323e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      ObjCDeclSpec ProtocolPropertyODS;
324e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
325e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      // and ObjCPropertyDecl::PropertyAttributeKind have identical
326e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      // values.  Should consolidate both into one enum type.
327e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      ProtocolPropertyODS.
328e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
329e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                            PIkind);
330a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      // Must re-establish the context from class extension to primary
331a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      // class context.
3327939418cbdf1b610a69e6adf29cd7dc8d0e9eaf8Fariborz Jahanian      ContextRAII SavedContext(*this, CCPrimary);
3337939418cbdf1b610a69e6adf29cd7dc8d0e9eaf8Fariborz Jahanian
334d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *ProtocolPtrTy =
33577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
336e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                      PIDecl->getGetterName(),
337e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                      PIDecl->getSetterName(),
338a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                      isOverridingProperty,
3394a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek                      MethodImplKind,
3404a2e9ea0c191176ebe996516a9cf4f93b7e932ccTed Kremenek                      /* lexicalDC = */ CDecl);
341d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
34228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
343e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    PIDecl->makeitReadWriteAttribute();
344e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_retain)
345e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
346f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong)
347f85e193739c953358c865005855253af4f68a497John McCall      PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
348e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_copy)
349e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
350e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    PIDecl->setSetterName(SetterSel);
351e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  } else {
352788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    // Tailor the diagnostics for the common case where a readwrite
353788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    // property is declared both in the @interface and the continuation.
354788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    // This is a common error where the user often intended the original
355788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    // declaration to be readonly.
356788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    unsigned diag =
357788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek      (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
358788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek      (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
359788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek      ? diag::err_use_continuation_class_redeclaration_readwrite
360788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek      : diag::err_use_continuation_class;
361788f489a37bf1150deb9378cec6a6eb902d0d1b2Ted Kremenek    Diag(AtLoc, diag)
362e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek      << CCPrimary->getDeclName();
363e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek    Diag(PIDecl->getLocation(), diag::note_property_declare);
364e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  }
365e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  *isOverridingProperty = true;
366e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  // Make sure setter decl is synthesized, and added to primary class's list.
3678254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek  ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
368d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis  PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
369d7c15a64ba8ebdca0dd48dd1d2f233107d34494eArgyrios Kyrtzidis  PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
370c80553efdc269dabd4ed0c3210555f45729ab5ddArgyrios Kyrtzidis  if (ASTMutationListener *L = Context.getASTMutationListener())
371c80553efdc269dabd4ed0c3210555f45729ab5ddArgyrios Kyrtzidis    L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
372d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
373e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek}
374e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
375e3d67bcc7b89b335dbcd097a299658057ea31097Ted KremenekObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
376e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           ObjCContainerDecl *CDecl,
377e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           SourceLocation AtLoc,
37877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                           SourceLocation LParenLoc,
379e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           FieldDeclarator &FD,
380e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           Selector GetterSel,
381e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           Selector SetterSel,
382e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           const bool isAssign,
383e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           const bool isReadWrite,
384e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek                                           const unsigned Attributes,
385dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                                           const unsigned AttributesAsWritten,
38683a230c83a54190366138c1a4f4310ef838b88fcJohn McCall                                           TypeSourceInfo *TInfo,
38723173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek                                           tok::ObjCKeywordKind MethodImplKind,
38823173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek                                           DeclContext *lexicalDC){
389e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  IdentifierInfo *PropertyId = FD.D.getIdentifier();
39083a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  QualType T = TInfo->getType();
39128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
39228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Issue a warning if property is 'assign' as default and its object, which is
39328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // gc'able conforms to NSCopying protocol
3944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() != LangOptions::NonGC &&
39528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
396c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (const ObjCObjectPointerType *ObjPtrTy =
397c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          T->getAs<ObjCObjectPointerType>()) {
398c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
399c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (IDecl)
400c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        if (ObjCProtocolDecl* PNSCopying =
401c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall            LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
402c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall          if (IDecl->ClassImplementsProtocol(PNSCopying, true))
403c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall            Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
40428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
405c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (T->isObjCObjectType())
40628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
40728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
408e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  DeclContext *DC = cast<DeclContext>(CDecl);
40928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
41028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                                                     FD.D.getIdentifierLoc(),
41177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                                     PropertyId, AtLoc, LParenLoc, TInfo);
412e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek
4139f550ff05d496e6b9480e5619a21d9da0c9e27c1Ted Kremenek  if (ObjCPropertyDecl *prevDecl =
4149f550ff05d496e6b9480e5619a21d9da0c9e27c1Ted Kremenek        ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
41528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(PDecl->getLocation(), diag::err_duplicate_property);
416894ae6ad081465a3b168db7e60ab3f94e3730b57Ted Kremenek    Diag(prevDecl->getLocation(), diag::note_property_declare);
41728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setInvalidDecl();
41828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
41923173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek  else {
42028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    DC->addDecl(PDecl);
42123173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek    if (lexicalDC)
42223173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      PDecl->setLexicalDeclContext(lexicalDC);
42323173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek  }
42428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
42528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (T->isArrayType() || T->isFunctionType()) {
42628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(AtLoc, diag::err_property_type) << T;
42728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setInvalidDecl();
42828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
42928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
43028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ProcessDeclAttributes(S, PDecl, FD.D);
43128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
43228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Regardless of setter/getter attribute, we save the default getter/setter
43328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // selector names in anticipation of declaration of setter/getter methods.
43428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  PDecl->setGetterName(GetterSel);
43528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  PDecl->setSetterName(SetterSel);
4360a68dc7f04be1542ce249ff4adb169453698ad91Argyrios Kyrtzidis  PDecl->setPropertyAttributesAsWritten(
437dbbdec994f5a96b5c25aaa679cd86ecabf545f7bArgyrios Kyrtzidis                          makePropertyAttributesAsWritten(AttributesAsWritten));
4380a68dc7f04be1542ce249ff4adb169453698ad91Argyrios Kyrtzidis
43928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
44028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
44128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
44228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_getter)
44328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
44428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
44528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_setter)
44628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
44728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
44828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (isReadWrite)
44928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
45028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
45128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_retain)
45228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
45328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
454f85e193739c953358c865005855253af4f68a497John McCall  if (Attributes & ObjCDeclSpec::DQ_PR_strong)
455f85e193739c953358c865005855253af4f68a497John McCall    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
456f85e193739c953358c865005855253af4f68a497John McCall
457f85e193739c953358c865005855253af4f68a497John McCall  if (Attributes & ObjCDeclSpec::DQ_PR_weak)
458f85e193739c953358c865005855253af4f68a497John McCall    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
459f85e193739c953358c865005855253af4f68a497John McCall
46028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_copy)
46128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
46228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
463f85e193739c953358c865005855253af4f68a497John McCall  if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
464f85e193739c953358c865005855253af4f68a497John McCall    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
465f85e193739c953358c865005855253af4f68a497John McCall
46628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (isAssign)
46728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
46828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
469265941bc308d65cc270d5c4de5806f37ce405606John McCall  // In the semantic attributes, one of nonatomic or atomic is always set.
47028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
47128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
472265941bc308d65cc270d5c4de5806f37ce405606John McCall  else
47345937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
47428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
475f85e193739c953358c865005855253af4f68a497John McCall  // 'unsafe_unretained' is alias for 'assign'.
476f85e193739c953358c865005855253af4f68a497John McCall  if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
477f85e193739c953358c865005855253af4f68a497John McCall    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
478f85e193739c953358c865005855253af4f68a497John McCall  if (isAssign)
479f85e193739c953358c865005855253af4f68a497John McCall    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
480f85e193739c953358c865005855253af4f68a497John McCall
48128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (MethodImplKind == tok::objc_required)
48228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
48328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  else if (MethodImplKind == tok::objc_optional)
48428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
48528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
486e3d67bcc7b89b335dbcd097a299658057ea31097Ted Kremenek  return PDecl;
48728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek}
48828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
489f85e193739c953358c865005855253af4f68a497John McCallstatic void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
490f85e193739c953358c865005855253af4f68a497John McCall                                 ObjCPropertyDecl *property,
491f85e193739c953358c865005855253af4f68a497John McCall                                 ObjCIvarDecl *ivar) {
492f85e193739c953358c865005855253af4f68a497John McCall  if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
493f85e193739c953358c865005855253af4f68a497John McCall
494f85e193739c953358c865005855253af4f68a497John McCall  QualType ivarType = ivar->getType();
495f85e193739c953358c865005855253af4f68a497John McCall  Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
496f85e193739c953358c865005855253af4f68a497John McCall
497265941bc308d65cc270d5c4de5806f37ce405606John McCall  // The lifetime implied by the property's attributes.
498265941bc308d65cc270d5c4de5806f37ce405606John McCall  Qualifiers::ObjCLifetime propertyLifetime =
499265941bc308d65cc270d5c4de5806f37ce405606John McCall    getImpliedARCOwnership(property->getPropertyAttributes(),
500265941bc308d65cc270d5c4de5806f37ce405606John McCall                           property->getType());
501f85e193739c953358c865005855253af4f68a497John McCall
502265941bc308d65cc270d5c4de5806f37ce405606John McCall  // We're fine if they match.
503265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (propertyLifetime == ivarLifetime) return;
504f85e193739c953358c865005855253af4f68a497John McCall
505265941bc308d65cc270d5c4de5806f37ce405606John McCall  // These aren't valid lifetimes for object ivars;  don't diagnose twice.
506265941bc308d65cc270d5c4de5806f37ce405606John McCall  if (ivarLifetime == Qualifiers::OCL_None ||
507265941bc308d65cc270d5c4de5806f37ce405606John McCall      ivarLifetime == Qualifiers::OCL_Autoreleasing)
508265941bc308d65cc270d5c4de5806f37ce405606John McCall    return;
509265941bc308d65cc270d5c4de5806f37ce405606John McCall
510265941bc308d65cc270d5c4de5806f37ce405606John McCall  switch (propertyLifetime) {
511265941bc308d65cc270d5c4de5806f37ce405606John McCall  case Qualifiers::OCL_Strong:
512265941bc308d65cc270d5c4de5806f37ce405606John McCall    S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
513265941bc308d65cc270d5c4de5806f37ce405606John McCall      << property->getDeclName()
514265941bc308d65cc270d5c4de5806f37ce405606John McCall      << ivar->getDeclName()
515265941bc308d65cc270d5c4de5806f37ce405606John McCall      << ivarLifetime;
516265941bc308d65cc270d5c4de5806f37ce405606John McCall    break;
517265941bc308d65cc270d5c4de5806f37ce405606John McCall
518265941bc308d65cc270d5c4de5806f37ce405606John McCall  case Qualifiers::OCL_Weak:
519265941bc308d65cc270d5c4de5806f37ce405606John McCall    S.Diag(propertyImplLoc, diag::error_weak_property)
520265941bc308d65cc270d5c4de5806f37ce405606John McCall      << property->getDeclName()
521265941bc308d65cc270d5c4de5806f37ce405606John McCall      << ivar->getDeclName();
522265941bc308d65cc270d5c4de5806f37ce405606John McCall    break;
523265941bc308d65cc270d5c4de5806f37ce405606John McCall
524265941bc308d65cc270d5c4de5806f37ce405606John McCall  case Qualifiers::OCL_ExplicitNone:
525265941bc308d65cc270d5c4de5806f37ce405606John McCall    S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
526265941bc308d65cc270d5c4de5806f37ce405606John McCall      << property->getDeclName()
527265941bc308d65cc270d5c4de5806f37ce405606John McCall      << ivar->getDeclName()
528265941bc308d65cc270d5c4de5806f37ce405606John McCall      << ((property->getPropertyAttributesAsWritten()
529265941bc308d65cc270d5c4de5806f37ce405606John McCall           & ObjCPropertyDecl::OBJC_PR_assign) != 0);
530265941bc308d65cc270d5c4de5806f37ce405606John McCall    break;
531265941bc308d65cc270d5c4de5806f37ce405606John McCall
532265941bc308d65cc270d5c4de5806f37ce405606John McCall  case Qualifiers::OCL_Autoreleasing:
533265941bc308d65cc270d5c4de5806f37ce405606John McCall    llvm_unreachable("properties cannot be autoreleasing");
534265941bc308d65cc270d5c4de5806f37ce405606John McCall
535265941bc308d65cc270d5c4de5806f37ce405606John McCall  case Qualifiers::OCL_None:
536265941bc308d65cc270d5c4de5806f37ce405606John McCall    // Any other property should be ignored.
537f85e193739c953358c865005855253af4f68a497John McCall    return;
538f85e193739c953358c865005855253af4f68a497John McCall  }
539f85e193739c953358c865005855253af4f68a497John McCall
540f85e193739c953358c865005855253af4f68a497John McCall  S.Diag(property->getLocation(), diag::note_property_declare);
541f85e193739c953358c865005855253af4f68a497John McCall}
542f85e193739c953358c865005855253af4f68a497John McCall
543015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian/// setImpliedPropertyAttributeForReadOnlyProperty -
544015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian/// This routine evaludates life-time attributes for a 'readonly'
545015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian/// property with no known lifetime of its own, using backing
546015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian/// 'ivar's attribute, if any. If no backing 'ivar', property's
547015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian/// life-time is assumed 'strong'.
548015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanianstatic void setImpliedPropertyAttributeForReadOnlyProperty(
549015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian              ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
550015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  Qualifiers::ObjCLifetime propertyLifetime =
551015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    getImpliedARCOwnership(property->getPropertyAttributes(),
552015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                           property->getType());
553015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  if (propertyLifetime != Qualifiers::OCL_None)
554015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    return;
555015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian
556015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  if (!ivar) {
557015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // if no backing ivar, make property 'strong'.
558015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
559015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    return;
560015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  }
561015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  // property assumes owenership of backing ivar.
562015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  QualType ivarType = ivar->getType();
563015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
564015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  if (ivarLifetime == Qualifiers::OCL_Strong)
565015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
566015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  else if (ivarLifetime == Qualifiers::OCL_Weak)
567015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
568015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  return;
569015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian}
57028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
57128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek/// ActOnPropertyImplDecl - This routine performs semantic checks and
57228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek/// builds the AST node for a property implementation declaration; declared
57328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek/// as @synthesize or @dynamic.
57428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek///
575d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Sema::ActOnPropertyImplDecl(Scope *S,
576d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                  SourceLocation AtLoc,
577d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                  SourceLocation PropertyLoc,
578d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                  bool Synthesize,
579d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                  IdentifierInfo *PropertyId,
580a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  IdentifierInfo *PropertyIvar,
581a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  SourceLocation PropertyIvarLoc) {
582e9686572f17968048a5e097254f85c202ea44aecTed Kremenek  ObjCContainerDecl *ClassImpDecl =
58384e0ccff7a6e51d7b81fd58686b493a4880dd44dFariborz Jahanian    dyn_cast<ObjCContainerDecl>(CurContext);
58428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Make sure we have a context for the property implementation declaration.
58528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (!ClassImpDecl) {
58628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(AtLoc, diag::error_missing_property_context);
587d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
58828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
589f911242f43ae1b0a85c323631fe817df95c9cbe9Argyrios Kyrtzidis  if (PropertyIvarLoc.isInvalid())
590f911242f43ae1b0a85c323631fe817df95c9cbe9Argyrios Kyrtzidis    PropertyIvarLoc = PropertyLoc;
591e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman  SourceLocation PropertyDiagLoc = PropertyLoc;
592e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman  if (PropertyDiagLoc.isInvalid())
593e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    PropertyDiagLoc = ClassImpDecl->getLocStart();
59428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCPropertyDecl *property = 0;
59528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCInterfaceDecl* IDecl = 0;
59628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Find the class or category class where this property must have
59728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // a declaration.
59828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCImplementationDecl *IC = 0;
59928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCCategoryImplDecl* CatImplClass = 0;
60028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
60128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    IDecl = IC->getClassInterface();
60228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // We always synthesize an interface for an implementation
60328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // without an interface decl. So, IDecl is always non-zero.
60428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    assert(IDecl &&
60528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek           "ActOnPropertyImplDecl - @implementation without @interface");
60628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
60728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // Look for this property declaration in the @implementation's @interface
60828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    property = IDecl->FindPropertyDeclaration(PropertyId);
60928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!property) {
61028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
611d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
61228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
613dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian    unsigned PIkind = property->getPropertyAttributesAsWritten();
61445937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
61545937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian                   ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
616dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian      if (AtLoc.isValid())
617dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian        Diag(AtLoc, diag::warn_implicit_atomic_property);
618dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian      else
619dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian        Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
620dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian      Diag(property->getLocation(), diag::note_property_declare);
621dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian    }
622dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian
62328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (const ObjCCategoryDecl *CD =
62428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
62528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      if (!CD->IsClassExtension()) {
62628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
62728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        Diag(property->getLocation(), diag::note_property_declare);
628d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        return 0;
62928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
63028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
63128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
63228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Synthesize) {
63328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(AtLoc, diag::error_synthesize_category_decl);
634d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
63528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
63628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    IDecl = CatImplClass->getClassInterface();
63728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!IDecl) {
63828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(AtLoc, diag::error_missing_property_interface);
639d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
64028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
64128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    ObjCCategoryDecl *Category =
64228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
64328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
64428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // If category for this implementation not found, it is an error which
64528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // has already been reported eralier.
64628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!Category)
647d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
64828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // Look for this property declaration in @implementation's category
64928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    property = Category->FindPropertyDeclaration(PropertyId);
65028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!property) {
65128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(PropertyLoc, diag::error_bad_category_property_decl)
65228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      << Category->getDeclName();
653d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
65428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
65528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else {
65628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(AtLoc, diag::error_bad_property_context);
657d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
65828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
65928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCIvarDecl *Ivar = 0;
660e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman  bool CompleteTypeErr = false;
661744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian  bool compat = true;
66228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Check that we have a valid, previously declared ivar for @synthesize
66328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Synthesize) {
66428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // @synthesize
66528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!PropertyIvar)
66628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      PropertyIvar = PropertyId;
667015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // Check that this is a previously declared 'ivar' in 'IDecl' interface
668015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    ObjCInterfaceDecl *ClassDeclared;
669015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
670265941bc308d65cc270d5c4de5806f37ce405606John McCall    QualType PropType = property->getType();
671265941bc308d65cc270d5c4de5806f37ce405606John McCall    QualType PropertyIvarType = PropType.getNonReferenceType();
672e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman
673e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
674d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            diag::err_incomplete_synthesized_property,
675d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            property->getDeclName())) {
676e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman      Diag(property->getLocation(), diag::note_property_declare);
677e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman      CompleteTypeErr = true;
678e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    }
679e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman
6804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount &&
681015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian        (property->getPropertyAttributesAsWritten() &
6823efd348d3586b22bb0153f17181128a1b3c41140Fariborz Jahanian         ObjCPropertyDecl::OBJC_PR_readonly) &&
6833efd348d3586b22bb0153f17181128a1b3c41140Fariborz Jahanian        PropertyIvarType->isObjCRetainableType()) {
684015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
685015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    }
686015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian
687015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    ObjCPropertyDecl::PropertyAttributeKind kind
688015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      = property->getPropertyAttributes();
689265941bc308d65cc270d5c4de5806f37ce405606John McCall
690265941bc308d65cc270d5c4de5806f37ce405606John McCall    // Add GC __weak to the ivar type if the property is weak.
691265941bc308d65cc270d5c4de5806f37ce405606John McCall    if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
6924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        getLangOpts().getGC() != LangOptions::NonGC) {
6934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      assert(!getLangOpts().ObjCAutoRefCount);
694265941bc308d65cc270d5c4de5806f37ce405606John McCall      if (PropertyIvarType.isObjCGCStrong()) {
695e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
696265941bc308d65cc270d5c4de5806f37ce405606John McCall        Diag(property->getLocation(), diag::note_property_declare);
697265941bc308d65cc270d5c4de5806f37ce405606John McCall      } else {
698265941bc308d65cc270d5c4de5806f37ce405606John McCall        PropertyIvarType =
699265941bc308d65cc270d5c4de5806f37ce405606John McCall          Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
700edc08821d3f73514291b339db29aca1a17485e39Fariborz Jahanian      }
701edc08821d3f73514291b339db29aca1a17485e39Fariborz Jahanian    }
702265941bc308d65cc270d5c4de5806f37ce405606John McCall
70328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (!Ivar) {
704265941bc308d65cc270d5c4de5806f37ce405606John McCall      // In ARC, give the ivar a lifetime qualifier based on the
705f85e193739c953358c865005855253af4f68a497John McCall      // property attributes.
7064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount &&
707265941bc308d65cc270d5c4de5806f37ce405606John McCall          !PropertyIvarType.getObjCLifetime() &&
708265941bc308d65cc270d5c4de5806f37ce405606John McCall          PropertyIvarType->isObjCRetainableType()) {
709f85e193739c953358c865005855253af4f68a497John McCall
710265941bc308d65cc270d5c4de5806f37ce405606John McCall        // It's an error if we have to do this and the user didn't
711265941bc308d65cc270d5c4de5806f37ce405606John McCall        // explicitly write an ownership attribute on the property.
712473506bd9dfd84944c2c5ca2c2a38814f46febc6Argyrios Kyrtzidis        if (!property->hasWrittenStorageAttribute() &&
713265941bc308d65cc270d5c4de5806f37ce405606John McCall            !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
714e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman          Diag(PropertyDiagLoc,
715473506bd9dfd84944c2c5ca2c2a38814f46febc6Argyrios Kyrtzidis               diag::err_arc_objc_property_default_assign_on_object);
716473506bd9dfd84944c2c5ca2c2a38814f46febc6Argyrios Kyrtzidis          Diag(property->getLocation(), diag::note_property_declare);
717265941bc308d65cc270d5c4de5806f37ce405606John McCall        } else {
718265941bc308d65cc270d5c4de5806f37ce405606John McCall          Qualifiers::ObjCLifetime lifetime =
719265941bc308d65cc270d5c4de5806f37ce405606John McCall            getImpliedARCOwnership(kind, PropertyIvarType);
720265941bc308d65cc270d5c4de5806f37ce405606John McCall          assert(lifetime && "no lifetime for property?");
7216dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian          if (lifetime == Qualifiers::OCL_Weak) {
7226dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian            bool err = false;
7236dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian            if (const ObjCObjectPointerType *ObjT =
7246dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian                PropertyIvarType->getAs<ObjCObjectPointerType>())
7256dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian              if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
726e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman                Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
7276dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian                Diag(property->getLocation(), diag::note_property_declare);
7286dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian                err = true;
7296dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian              }
7304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie            if (!err && !getLangOpts().ObjCRuntimeHasWeak) {
731e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman              Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
7326dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian              Diag(property->getLocation(), diag::note_property_declare);
7336dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian            }
734f85e193739c953358c865005855253af4f68a497John McCall          }
7356dce88d9f71cd4be18554af94145a9147b363199Fariborz Jahanian
736f85e193739c953358c865005855253af4f68a497John McCall          Qualifiers qs;
737265941bc308d65cc270d5c4de5806f37ce405606John McCall          qs.addObjCLifetime(lifetime);
738f85e193739c953358c865005855253af4f68a497John McCall          PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
739f85e193739c953358c865005855253af4f68a497John McCall        }
740f85e193739c953358c865005855253af4f68a497John McCall      }
741f85e193739c953358c865005855253af4f68a497John McCall
742f85e193739c953358c865005855253af4f68a497John McCall      if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
7434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !getLangOpts().ObjCAutoRefCount &&
7444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          getLangOpts().getGC() == LangOptions::NonGC) {
745e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
746f85e193739c953358c865005855253af4f68a497John McCall        Diag(property->getLocation(), diag::note_property_declare);
747f85e193739c953358c865005855253af4f68a497John McCall      }
748f85e193739c953358c865005855253af4f68a497John McCall
749ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
750f911242f43ae1b0a85c323631fe817df95c9cbe9Argyrios Kyrtzidis                                  PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
75114086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian                                  PropertyIvarType, /*Dinfo=*/0,
7527504966cc0d07eac26baabec80a7406695efb307Fariborz Jahanian                                  ObjCIvarDecl::Private,
753ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian                                  (Expr *)0, true);
754e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman      if (CompleteTypeErr)
755e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Ivar->setInvalidDecl();
75629fa69addf8b19871e7866c32c92b5d142c4bfbcDaniel Dunbar      ClassImpDecl->addDecl(Ivar);
7571b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith      IDecl->makeDeclVisibleInContext(Ivar);
75828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      property->setPropertyIvarDecl(Ivar);
75928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
7604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().ObjCNonFragileABI)
761e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
762e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman            << PropertyId;
76328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      // Note! I deliberately want it to fall thru so, we have a
76428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      // a property implementation and to avoid future warnings.
7654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (getLangOpts().ObjCNonFragileABI &&
76660ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor               !declaresSameEntity(ClassDeclared, IDecl)) {
767e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman      Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
76828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      << property->getDeclName() << Ivar->getDeclName()
76928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      << ClassDeclared->getDeclName();
77028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
7714087f27e5416c799bcb6be072f905be752acb61cDaniel Dunbar      << Ivar << Ivar->getName();
77228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      // Note! I deliberately want it to fall thru so more errors are caught.
77328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
77428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    QualType IvarType = Context.getCanonicalType(Ivar->getType());
77528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
77628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // Check that type of property and its ivar are type compatible.
777744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian    if (!Context.hasSameType(PropertyIvarType, IvarType)) {
778744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian      compat = false;
77914086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian      if (isa<ObjCObjectPointerType>(PropertyIvarType)
780f85e193739c953358c865005855253af4f68a497John McCall          && isa<ObjCObjectPointerType>(IvarType))
78162ac5d01aade35790a6d8e814edb21062da5d3f7Fariborz Jahanian        compat =
78262ac5d01aade35790a6d8e814edb21062da5d3f7Fariborz Jahanian          Context.canAssignObjCInterfaces(
78314086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian                                  PropertyIvarType->getAs<ObjCObjectPointerType>(),
78462ac5d01aade35790a6d8e814edb21062da5d3f7Fariborz Jahanian                                  IvarType->getAs<ObjCObjectPointerType>());
785b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor      else {
786f911242f43ae1b0a85c323631fe817df95c9cbe9Argyrios Kyrtzidis        compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
787f911242f43ae1b0a85c323631fe817df95c9cbe9Argyrios Kyrtzidis                                             IvarType)
788daa8e4e888758d55a7a759dd4a91b83921cef222John McCall                    == Compatible);
789b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor      }
79062ac5d01aade35790a6d8e814edb21062da5d3f7Fariborz Jahanian      if (!compat) {
791e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_property_ivar_type)
792f921a4868cf1876636f6684e7f68697b18c0cb47Ted Kremenek          << property->getDeclName() << PropType
793f921a4868cf1876636f6684e7f68697b18c0cb47Ted Kremenek          << Ivar->getDeclName() << IvarType;
794f921a4868cf1876636f6684e7f68697b18c0cb47Ted Kremenek        Diag(Ivar->getLocation(), diag::note_ivar_decl);
79528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        // Note! I deliberately want it to fall thru so, we have a
79628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        // a property implementation and to avoid future warnings.
79728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
798744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian      else {
799744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        // FIXME! Rules for properties are somewhat different that those
800744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        // for assignments. Use a new routine to consolidate all cases;
801744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        // specifically for property redeclarations as well as for ivars.
802744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
803744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
804744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        if (lhsType != rhsType &&
805744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian            lhsType->isArithmeticType()) {
806744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian          Diag(PropertyDiagLoc, diag::error_property_ivar_type)
807744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian            << property->getDeclName() << PropType
808744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian            << Ivar->getDeclName() << IvarType;
809744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian          Diag(Ivar->getLocation(), diag::note_ivar_decl);
810744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian          // Fall thru - see previous comment
811744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian        }
81228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
81328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      // __weak is explicit. So it works on Canonical type.
814f85e193739c953358c865005855253af4f68a497John McCall      if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
8154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           getLangOpts().getGC() != LangOptions::NonGC)) {
816e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_weak_property)
81728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << property->getDeclName() << Ivar->getDeclName();
818edc08821d3f73514291b339db29aca1a17485e39Fariborz Jahanian        Diag(Ivar->getLocation(), diag::note_ivar_decl);
81928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        // Fall thru - see previous comment
82028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
821f85e193739c953358c865005855253af4f68a497John McCall      // Fall thru - see previous comment
82228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      if ((property->getType()->isObjCObjectPointerType() ||
82328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek           PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
8244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          getLangOpts().getGC() != LangOptions::NonGC) {
825e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_strong_property)
82628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << property->getDeclName() << Ivar->getDeclName();
82728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        // Fall thru - see previous comment
82828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
82928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
8304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount)
831f85e193739c953358c865005855253af4f68a497John McCall      checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
83228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else if (PropertyIvar)
83328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // @dynamic
834e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
835f85e193739c953358c865005855253af4f68a497John McCall
83628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  assert (property && "ActOnPropertyImplDecl - property declaration missing");
83728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCPropertyImplDecl *PIDecl =
83828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
83928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                               property,
84028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                               (Synthesize ?
84128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                                ObjCPropertyImplDecl::Synthesize
84228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                                : ObjCPropertyImplDecl::Dynamic),
843a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                               Ivar, PropertyIvarLoc);
844e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman
845744147138cd2a77320c431578da130cc3d81a1d5Fariborz Jahanian  if (CompleteTypeErr || !compat)
846e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    PIDecl->setInvalidDecl();
847e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman
84817cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian  if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
84917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian    getterMethod->createImplicitParams(Context, IDecl);
850e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
8510313f441b72ef6b69a93e5003c684b01cb72fd46Fariborz Jahanian        Ivar->getType()->isRecordType()) {
85217cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      // For Objective-C++, need to synthesize the AST for the IVAR object to be
85317cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      // returned by the getter as it must conform to C++'s copy-return rules.
85417cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      // FIXME. Eventually we want to do this for Objective-C as well.
85517cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
85617cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      DeclRefExpr *SelfExpr =
857f4b88a45902af1802a1cb42ba48b1c474474f228John McCall        new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
858f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                  VK_RValue, SourceLocation());
85917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      Expr *IvarRefExpr =
86017cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
86117cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian                                      SelfExpr, true, true);
86260d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res =
86317cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        PerformCopyInitialization(InitializedEntity::InitializeResult(
8643c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                                    SourceLocation(),
8653c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                                    getterMethod->getResultType(),
8663c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor                                    /*NRVO=*/false),
86717cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian                                  SourceLocation(),
86817cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian                                  Owned(IvarRefExpr));
86917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      if (!Res.isInvalid()) {
87017cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        Expr *ResExpr = Res.takeAs<Expr>();
87117cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        if (ResExpr)
8724765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall          ResExpr = MaybeCreateExprWithCleanups(ResExpr);
87317cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        PIDecl->setGetterCXXConstructor(ResExpr);
87417cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      }
87517cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian    }
876831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
877831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian        !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
878831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian      Diag(getterMethod->getLocation(),
879831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian           diag::warn_property_getter_owning_mismatch);
880831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian      Diag(property->getLocation(), diag::note_property_declare);
881831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    }
88217cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian  }
88317cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian  if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
88417cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian    setterMethod->createImplicitParams(Context, IDecl);
885e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman    if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
886e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Ivar->getType()->isRecordType()) {
88717cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      // FIXME. Eventually we want to do this for Objective-C as well.
88817cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
88917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      DeclRefExpr *SelfExpr =
890f4b88a45902af1802a1cb42ba48b1c474474f228John McCall        new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
891f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                  VK_RValue, SourceLocation());
89217cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      Expr *lhs =
89317cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian        new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
89417cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian                                      SelfExpr, true, true);
89517cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
89617cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      ParmVarDecl *Param = (*P);
8973c3b7f90a863af43fa63043d396553ecf205351cJohn McCall      QualType T = Param->getType().getNonReferenceType();
898f4b88a45902af1802a1cb42ba48b1c474474f228John McCall      Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
899f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                            VK_LValue, SourceLocation());
900fa432391a708448518df1e6ced4e34f4c349d169Fariborz Jahanian      ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
9012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                  BO_Assign, lhs, rhs);
90257e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian      if (property->getPropertyAttributes() &
90357e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian          ObjCPropertyDecl::OBJC_PR_atomic) {
90457e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian        Expr *callExpr = Res.takeAs<Expr>();
90557e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian        if (const CXXOperatorCallExpr *CXXCE =
90613bf6336ab395eb6af487ab96d32229460907769Fariborz Jahanian              dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
90713bf6336ab395eb6af487ab96d32229460907769Fariborz Jahanian          if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
90857e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian            if (!FuncDecl->isTrivial())
90920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian              if (property->getType()->isReferenceType()) {
91020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                Diag(PropertyLoc,
91120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                     diag::err_atomic_property_nontrivial_assign_op)
91257e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian                    << property->getType();
91320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                Diag(FuncDecl->getLocStart(),
91420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian                     diag::note_callee_decl) << FuncDecl;
91520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian              }
91657e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian      }
91717cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian      PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
91817cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian    }
91917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian  }
92017cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanian
92128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (IC) {
92228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Synthesize)
92328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      if (ObjCPropertyImplDecl *PPIDecl =
92428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek          IC->FindPropertyImplIvarDecl(PropertyIvar)) {
92528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        Diag(PropertyLoc, diag::error_duplicate_ivar_use)
92628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
92728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << PropertyIvar;
92828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        Diag(PPIDecl->getLocation(), diag::note_previous_use);
92928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
93028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
93128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (ObjCPropertyImplDecl *PPIDecl
93228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        = IC->FindPropertyImplDecl(PropertyId)) {
93328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
93428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
935d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
93628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
93728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    IC->addPropertyImplementation(PIDecl);
9384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCDefaultSynthProperties &&
9394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        getLangOpts().ObjCNonFragileABI2 &&
94071207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek        !IDecl->isObjCRequiresPropertyDefs()) {
941ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      // Diagnose if an ivar was lazily synthesdized due to a previous
942ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      // use and if 1) property is @dynamic or 2) property is synthesized
943cdaa6a8fed16d8bd3987fb4f3304dfb4e52876c3Fariborz Jahanian      // but it requires an ivar of different name.
944411c25c4512f00e07442e8818d011c7e0902d739Fariborz Jahanian      ObjCInterfaceDecl *ClassDeclared=0;
945ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      ObjCIvarDecl *Ivar = 0;
946ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      if (!Synthesize)
947ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian        Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
948ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      else {
949ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian        if (PropertyIvar && PropertyIvar != PropertyId)
950ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian          Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
951ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      }
952cdaa6a8fed16d8bd3987fb4f3304dfb4e52876c3Fariborz Jahanian      // Issue diagnostics only if Ivar belongs to current class.
953cdaa6a8fed16d8bd3987fb4f3304dfb4e52876c3Fariborz Jahanian      if (Ivar && Ivar->getSynthesize() &&
95460ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor          declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
955ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian        Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
956ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian        << PropertyId;
957ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian        Ivar->setInvalidDecl();
958ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian      }
959ad51e74030a59a8aa4ef0ebca1d7a701602ef53bFariborz Jahanian    }
96028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else {
96128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Synthesize)
96228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      if (ObjCPropertyImplDecl *PPIDecl =
96328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek          CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
964e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman        Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
96528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
96628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << PropertyIvar;
96728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        Diag(PPIDecl->getLocation(), diag::note_previous_use);
96828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      }
96928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
97028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (ObjCPropertyImplDecl *PPIDecl =
97128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        CatImplClass->FindPropertyImplDecl(PropertyId)) {
972e4c043d3fdd0f98ab724aa4f1419cec9985ea2c4Eli Friedman      Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
97328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
974d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
97528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
97628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    CatImplClass->addPropertyImplementation(PIDecl);
97728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
97828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
979d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return PIDecl;
98028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek}
98128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
98228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek//===----------------------------------------------------------------------===//
98328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek// Helper methods.
98428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek//===----------------------------------------------------------------------===//
98528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
9869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// DiagnosePropertyMismatch - Compares two properties for their
9879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// attributes and types and warns on a variety of inconsistencies.
9889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
9899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid
9909d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekSema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
9919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                               ObjCPropertyDecl *SuperProperty,
9929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                               const IdentifierInfo *inheritedName) {
9939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCPropertyDecl::PropertyAttributeKind CAttr =
9949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  Property->getPropertyAttributes();
9959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCPropertyDecl::PropertyAttributeKind SAttr =
9969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  SuperProperty->getPropertyAttributes();
9979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
9989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
9999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Diag(Property->getLocation(), diag::warn_readonly_property)
10009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Property->getDeclName() << inheritedName;
10019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
10029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
10039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Diag(Property->getLocation(), diag::warn_property_attribute)
10049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Property->getDeclName() << "copy" << inheritedName;
10051b46d8d78be713c546fb9680eaeea8f793bee5bfFariborz Jahanian  else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1006f85e193739c953358c865005855253af4f68a497John McCall    unsigned CAttrRetain =
1007f85e193739c953358c865005855253af4f68a497John McCall      (CAttr &
1008f85e193739c953358c865005855253af4f68a497John McCall       (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1009f85e193739c953358c865005855253af4f68a497John McCall    unsigned SAttrRetain =
1010f85e193739c953358c865005855253af4f68a497John McCall      (SAttr &
1011f85e193739c953358c865005855253af4f68a497John McCall       (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1012f85e193739c953358c865005855253af4f68a497John McCall    bool CStrong = (CAttrRetain != 0);
1013f85e193739c953358c865005855253af4f68a497John McCall    bool SStrong = (SAttrRetain != 0);
1014f85e193739c953358c865005855253af4f68a497John McCall    if (CStrong != SStrong)
1015f85e193739c953358c865005855253af4f68a497John McCall      Diag(Property->getLocation(), diag::warn_property_attribute)
1016f85e193739c953358c865005855253af4f68a497John McCall        << Property->getDeclName() << "retain (or strong)" << inheritedName;
1017f85e193739c953358c865005855253af4f68a497John McCall  }
10189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
10209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
10219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Diag(Property->getLocation(), diag::warn_property_attribute)
10229d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Property->getDeclName() << "atomic" << inheritedName;
10239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (Property->getSetterName() != SuperProperty->getSetterName())
10249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Diag(Property->getLocation(), diag::warn_property_attribute)
10259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Property->getDeclName() << "setter" << inheritedName;
10269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (Property->getGetterName() != SuperProperty->getGetterName())
10279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Diag(Property->getLocation(), diag::warn_property_attribute)
10289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Property->getDeclName() << "getter" << inheritedName;
10299d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  QualType LHSType =
10319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Context.getCanonicalType(SuperProperty->getType());
10329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  QualType RHSType =
10339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    Context.getCanonicalType(Property->getType());
10349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1035c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian  if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
10368beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian    // Do cases not handled in above.
10378beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian    // FIXME. For future support of covariant property types, revisit this.
10388beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian    bool IncompatibleObjC = false;
10398beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian    QualType ConvertedType;
10408beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian    if (!isObjCPointerConversion(RHSType, LHSType,
10418beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian                                 ConvertedType, IncompatibleObjC) ||
104213546a871db44c707001f507a9edfd0d5bcc81b4Fariborz Jahanian        IncompatibleObjC) {
10438beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian        Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
10448beb6a2426b5a6b79ecf019316d9fbd30755e087Fariborz Jahanian        << Property->getType() << SuperProperty->getType() << inheritedName;
104513546a871db44c707001f507a9edfd0d5bcc81b4Fariborz Jahanian      Diag(SuperProperty->getLocation(), diag::note_property_declare);
104613546a871db44c707001f507a9edfd0d5bcc81b4Fariborz Jahanian    }
10479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
10489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
10499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekbool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
10519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                            ObjCMethodDecl *GetterMethod,
10529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                            SourceLocation Loc) {
10539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (GetterMethod &&
10543c3b7f90a863af43fa63043d396553ecf205351cJohn McCall      !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
10553c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                           property->getType().getNonReferenceType())) {
10569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    AssignConvertType result = Incompatible;
10571c23e91ef66688d20868b6bab3b5589a119eb603John McCall    if (property->getType()->isObjCObjectPointerType())
1058b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor      result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
10591c23e91ef66688d20868b6bab3b5589a119eb603John McCall                                          property->getType());
10609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (result != Compatible) {
10619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(Loc, diag::warn_accessor_property_type_mismatch)
10629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << property->getDeclName()
10639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << GetterMethod->getSelector();
10649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(GetterMethod->getLocation(), diag::note_declared_at);
10659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return true;
10669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
10679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
10689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return false;
10699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
10709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// ComparePropertiesInBaseAndSuper - This routine compares property
10729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// declarations in base and its super class, if any, and issues
1073fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// diagnostics in a variety of inconsistent situations.
10749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
10759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
10769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
10779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!SDecl)
10789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
10799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: O(N^2)
10809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
10819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = SDecl->prop_end(); S != E; ++S) {
1082262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *SuperPDecl = &*S;
10839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Does property in super class has declaration in current class?
10849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
10859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); I != E; ++I) {
1086262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *PDecl = &*I;
10879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
10889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          DiagnosePropertyMismatch(PDecl, SuperPDecl,
10899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                   SDecl->getIdentifier());
10909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
10919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
10929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
10939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
10959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// of properties declared in a protocol and compares their attribute against
10969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// the same property declared in the class or category.
10979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid
10989d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekSema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
10999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                          ObjCProtocolDecl *PDecl) {
11009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
11019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!IDecl) {
11029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Category
11039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
11049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    assert (CatDecl && "MatchOneProtocolPropertiesInClass");
11059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!CatDecl->IsClassExtension())
11069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
11079d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = PDecl->prop_end(); P != E; ++P) {
1108262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        ObjCPropertyDecl *Pr = &*P;
11099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        ObjCCategoryDecl::prop_iterator CP, CE;
11109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        // Is this property already in  category's list of properties?
11112d2f9368d35b3628c7e3b4563f74849a0f901a00Ted Kremenek        for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
1112262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          if (CP->getIdentifier() == Pr->getIdentifier())
11139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            break;
11149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        if (CP != CE)
11159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          // Property protocol already exist in class. Diagnose any mismatch.
1116262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
11179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
11189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
11199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
11219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = PDecl->prop_end(); P != E; ++P) {
1122262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *Pr = &*P;
11239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCInterfaceDecl::prop_iterator CP, CE;
11249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Is this property already in  class's list of properties?
11259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1126262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (CP->getIdentifier() == Pr->getIdentifier())
11279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        break;
11289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (CP != CE)
11299d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Property protocol already exist in class. Diagnose any mismatch.
1130262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
11319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
11329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
11339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// CompareProperties - This routine compares properties
11359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// declared in 'ClassOrProtocol' objects (which can be a class or an
11369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// inherited protocol with the list of properties for class/category 'CDecl'
11379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
1138d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1139d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClassDecl = ClassOrProtocol;
11409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
11419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!IDecl) {
11439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Category
11449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
11459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    assert (CatDecl && "CompareProperties");
11469d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
11479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
11489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = MDecl->protocol_end(); P != E; ++P)
11499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Match properties of category with those of protocol (*P)
11509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(CatDecl, *P);
11519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Go thru the list of protocols for this category and recursively match
11539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // their properties with those in the category.
11549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
11559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = CatDecl->protocol_end(); P != E; ++P)
1156d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        CompareProperties(CatDecl, *P);
11579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else {
11589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
11599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
11609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = MD->protocol_end(); P != E; ++P)
11619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        MatchOneProtocolPropertiesInClass(CatDecl, *P);
11629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
11639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
11649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
116753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
116853b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          P = MDecl->all_referenced_protocol_begin(),
116953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          E = MDecl->all_referenced_protocol_end(); P != E; ++P)
11709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Match properties of class IDecl with those of protocol (*P).
11719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(IDecl, *P);
11729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Go thru the list of protocols for this class and recursively match
11749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // their properties with those declared in the class.
117553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
117653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          P = IDecl->all_referenced_protocol_begin(),
117753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          E = IDecl->all_referenced_protocol_end(); P != E; ++P)
1178d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      CompareProperties(IDecl, *P);
11799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  } else {
11809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
11819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
11829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = MD->protocol_end(); P != E; ++P)
11839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(IDecl, *P);
11849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
11869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// isPropertyReadonly - Return true if property is readonly, by searching
11889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// for the property in the class and in its categories and implementations
11899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
11909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekbool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
11919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                              ObjCInterfaceDecl *IDecl) {
11929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // by far the most common case.
11939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!PDecl->isReadOnly())
11949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return false;
11959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Even if property is ready only, if interface has a user defined setter,
11969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // it is not considered read only.
11979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (IDecl->getInstanceMethod(PDecl->getSetterName()))
11989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return false;
11999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Main class has the property as 'readonly'. Must search
12019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // through the category list to see if the property's
12029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // attribute has been over-ridden to 'readwrite'.
12039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
12049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       Category; Category = Category->getNextClassCategory()) {
12059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Even if property is ready only, if a category has a user defined setter,
12069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // it is not considered read only.
12079d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (Category->getInstanceMethod(PDecl->getSetterName()))
12089d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl *P =
12109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Category->FindPropertyDeclaration(PDecl->getIdentifier());
12119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (P && !P->isReadOnly())
12129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Also, check for definition of a setter method in the implementation if
12169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // all else failed.
12179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
12189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ObjCImplementationDecl *IMD =
12199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
12209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (IMD->getInstanceMethod(PDecl->getSetterName()))
12219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return false;
12229d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else if (ObjCCategoryImplDecl *CIMD =
12239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek               dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
12249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (CIMD->getInstanceMethod(PDecl->getSetterName()))
12259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return false;
12269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
12279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Lastly, look through the implementation (if one is in scope).
12299d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
12309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
12319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // If all fails, look at the super class.
12339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
12349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return isPropertyReadonly(PDecl, SIDecl);
12359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return true;
12369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
12379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12389d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// CollectImmediateProperties - This routine collects all properties in
12399d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// the class and its conforming protocols; but not those it its super class.
12409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1241cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian            llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1242cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian            llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
12439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
12449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
12459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); P != E; ++P) {
1246262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
12479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      PropMap[Prop->getIdentifier()] = Prop;
12489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
12499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
125053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
125153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
125253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1253cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        CollectImmediateProperties((*PI), PropMap, SuperPropMap);
12549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
12569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!CATDecl->IsClassExtension())
12579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
12589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = CATDecl->prop_end(); P != E; ++P) {
1259262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        ObjCPropertyDecl *Prop = &*P;
12609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        PropMap[Prop->getIdentifier()] = Prop;
12619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
12629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
126353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
12649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = CATDecl->protocol_end(); PI != E; ++PI)
1265cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      CollectImmediateProperties((*PI), PropMap, SuperPropMap);
12669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
12689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
12699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->prop_end(); P != E; ++P) {
1270262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1271cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1272cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      // Exclude property for protocols which conform to class's super-class,
1273cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      // as super-class has to implement the property.
1274a929ec7b673b244bec024ba7eb95140404a7af5dFariborz Jahanian      if (!PropertyFromSuper ||
1275a929ec7b673b244bec024ba7eb95140404a7af5dFariborz Jahanian          PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
1276cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1277cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        if (!PropEntry)
1278cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian          PropEntry = Prop;
1279cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      }
12809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
12819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through protocol's protocols.
12829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
12839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->protocol_end(); PI != E; ++PI)
1284cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      CollectImmediateProperties((*PI), PropMap, SuperPropMap);
12859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
12879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1288509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// CollectClassPropertyImplementations - This routine collects list of
1289509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties to be implemented in the class. This includes, class's
1290509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// and its conforming protocols' properties.
1291509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanianstatic void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1292509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian                llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1293509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1294509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1295509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = IDecl->prop_end(); P != E; ++P) {
1296262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1297509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      PropMap[Prop->getIdentifier()] = Prop;
1298509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
129953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
130053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
130153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1302509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations((*PI), PropMap);
1303509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1304509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1305509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1306509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = PDecl->prop_end(); P != E; ++P) {
1307262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1308ac37150c13c4e6f54551c52a1118f105be5f136bFariborz Jahanian      if (!PropMap.count(Prop->getIdentifier()))
1309ac37150c13c4e6f54551c52a1118f105be5f136bFariborz Jahanian        PropMap[Prop->getIdentifier()] = Prop;
1310509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
1311509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // scan through protocol's protocols.
1312509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1313509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = PDecl->protocol_end(); PI != E; ++PI)
1314509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations((*PI), PropMap);
1315509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1316509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
1317509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1318509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// CollectSuperClassPropertyImplementations - This routine collects list of
1319509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties to be implemented in super class(s) and also coming from their
1320509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// conforming protocols.
1321509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanianstatic void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1322509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian                llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1323509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1324509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    while (SDecl) {
1325509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations(SDecl, PropMap);
1326509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      SDecl = SDecl->getSuperClass();
1327509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
1328509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1329509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
1330509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
13319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// LookupPropertyDecl - Looks up a property in the current class and all
13329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// its protocols.
13339d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
13349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                     IdentifierInfo *II) {
13359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (const ObjCInterfaceDecl *IDecl =
13369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        dyn_cast<ObjCInterfaceDecl>(CDecl)) {
13379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
13389d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); P != E; ++P) {
1339262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
13409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop->getIdentifier() == II)
13419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
134453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
134553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
134653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
13479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
13489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop)
13499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
13529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  else if (const ObjCProtocolDecl *PDecl =
13539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            dyn_cast<ObjCProtocolDecl>(CDecl)) {
13549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
13559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->prop_end(); P != E; ++P) {
1356262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
13579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop->getIdentifier() == II)
13589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through protocol's protocols.
13619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
13629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->protocol_end(); PI != E; ++PI) {
13639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
13649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop)
13659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
13689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return 0;
13699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
13709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1371d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenekstatic IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1372d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek                                                ASTContext &Ctx) {
1373f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<128> ivarName;
1374d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  {
1375d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek    llvm::raw_svector_ostream os(ivarName);
1376d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek    os << '_' << Prop->getIdentifier()->getName();
1377d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  }
1378d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  return &Ctx.Idents.get(ivarName.str());
1379d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek}
1380d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek
1381509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// DefaultSynthesizeProperties - This routine default synthesizes all
1382509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties which must be synthesized in class's @implementation.
1383d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenekvoid Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1384d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek                                       ObjCInterfaceDecl *IDecl) {
1385509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1386509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1387509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  CollectClassPropertyImplementations(IDecl, PropMap);
1388509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (PropMap.empty())
1389509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    return;
1390509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1391509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1392509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1393509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1394509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian       P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1395509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    ObjCPropertyDecl *Prop = P->second;
1396509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // If property to be implemented in the super class, ignore.
1397509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    if (SuperPropMap[Prop->getIdentifier()])
1398509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      continue;
1399509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // Is there a matching propery synthesize/dynamic?
1400509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    if (Prop->isInvalidDecl() ||
1401509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1402509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian        IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1403509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      continue;
1404d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian    // Property may have been synthesized by user.
1405d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian    if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1406d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian      continue;
140795f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian    if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
140895f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian      if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
140995f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian        continue;
141095f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian      if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
141195f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian        continue;
141295f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian    }
1413f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian    if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1414f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      // We won't auto-synthesize properties declared in protocols.
1415f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      Diag(IMPDecl->getLocation(),
1416f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian           diag::warn_auto_synthesizing_protocol_property);
1417f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      Diag(Prop->getLocation(), diag::note_property_declare);
1418f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      continue;
1419f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian    }
14202a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek
14212a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // We use invalid SourceLocations for the synthesized ivars since they
14222a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // aren't really synthesized at a particular location; they just exist.
14232a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // Saying that they are located at the @implementation isn't really going
14242a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // to help users.
1425975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1426975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian      ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1427975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            true,
1428975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            /* property = */ Prop->getIdentifier(),
1429975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1430975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            SourceLocation()));
1431975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    if (PIDecl) {
1432975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian      Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
14335ea66619a3108e2985e952b9a60a84316d464b25Fariborz Jahanian      Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1434975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    }
14352a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek  }
1436509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
14379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14388697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanianvoid Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
14398697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
14408697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian    return;
14418697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
14428697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (!IC)
14438697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian    return;
14448697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
144571207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek    if (!IDecl->isObjCRequiresPropertyDefs())
1446eb4f2c56c298071d58b441ccf801b039be93788aFariborz Jahanian      DefaultSynthesizeProperties(S, IC, IDecl);
14478697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian}
14488697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
144917cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanianvoid Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
14509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                      ObjCContainerDecl *CDecl,
14519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                      const llvm::DenseSet<Selector>& InsMap) {
1452cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1453cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1454cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian    CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1455cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian
14569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1457cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
14589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (PropMap.empty())
14599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
14609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
14629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCImplDecl::propimpl_iterator
14639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       I = IMPDecl->propimpl_begin(),
14649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       EI = IMPDecl->propimpl_end(); I != EI; ++I)
1465262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    PropImplMap.insert(I->getPropertyDecl());
14669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
14689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
14699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl *Prop = P->second;
14709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Is there a matching propery synthesize/dynamic?
14719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (Prop->isInvalidDecl() ||
14729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1473327126ee3d2faad9314b2633974eefc672f73b79Fariborz Jahanian        PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
14749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      continue;
14759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!InsMap.count(Prop->getGetterName())) {
1476b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(IMPDecl->getLocation(),
14779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           isa<ObjCCategoryDecl>(CDecl) ?
14789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            diag::warn_setter_getter_impl_required_in_category :
14799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            diag::warn_setter_getter_impl_required)
14809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Prop->getDeclName() << Prop->getGetterName();
1481b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(Prop->getLocation(),
1482b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian           diag::note_property_declare);
1483da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian      if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1484da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian        if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
148571207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek          if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1486da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian            Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1487da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian
14889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
14899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1491b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(IMPDecl->getLocation(),
14929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           isa<ObjCCategoryDecl>(CDecl) ?
14939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_setter_getter_impl_required_in_category :
14949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_setter_getter_impl_required)
14959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Prop->getDeclName() << Prop->getSetterName();
1496b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(Prop->getLocation(),
1497b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian           diag::note_property_declare);
1498da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian      if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1499da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian        if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
150071207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek          if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1501da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian            Diag(RID->getLocation(), diag::note_suppressed_class_declare);
15029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
15039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
15049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
15059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
15069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid
15079d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekSema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
15089d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       ObjCContainerDecl* IDecl) {
15099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Rules apply in non-GC mode only
15104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() != LangOptions::NonGC)
15119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
15129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
15139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = IDecl->prop_end();
15149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       I != E; ++I) {
1515262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *Property = &*I;
151694659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    ObjCMethodDecl *GetterMethod = 0;
151794659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    ObjCMethodDecl *SetterMethod = 0;
151894659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    bool LookedUpGetterSetter = false;
151994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
15209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    unsigned Attributes = Property->getPropertyAttributes();
1521265941bc308d65cc270d5c4de5806f37ce405606John McCall    unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
152294659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
1523265941bc308d65cc270d5c4de5806f37ce405606John McCall    if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1524265941bc308d65cc270d5c4de5806f37ce405606John McCall        !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
152594659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
152694659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
152794659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      LookedUpGetterSetter = true;
152894659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (GetterMethod) {
152994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(GetterMethod->getLocation(),
153094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis             diag::warn_default_atomic_custom_getter_setter)
1531293a45e724a15fb58b8805a5791f9f3aee769cf6Argyrios Kyrtzidis          << Property->getIdentifier() << 0;
153294659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(Property->getLocation(), diag::note_property_declare);
153394659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
153494659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (SetterMethod) {
153594659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(SetterMethod->getLocation(),
153694659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis             diag::warn_default_atomic_custom_getter_setter)
1537293a45e724a15fb58b8805a5791f9f3aee769cf6Argyrios Kyrtzidis          << Property->getIdentifier() << 1;
153894659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(Property->getLocation(), diag::note_property_declare);
153994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
154094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    }
154194659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
15429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // We only care about readwrite atomic property.
15439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
15449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
15459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      continue;
15469d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (const ObjCPropertyImplDecl *PIDecl
15479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
15489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
15499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        continue;
155094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (!LookedUpGetterSetter) {
155194659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
155294659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
155394659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        LookedUpGetterSetter = true;
155494659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
15559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
15569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        SourceLocation MethodLoc =
15579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          (GetterMethod ? GetterMethod->getLocation()
15589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                        : SetterMethod->getLocation());
15599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Diag(MethodLoc, diag::warn_atomic_property_rule)
15607d65f6965d215f4cb2eb0738ee1b9024e5ab5bbaFariborz Jahanian          << Property->getIdentifier() << (GetterMethod != 0)
15617d65f6965d215f4cb2eb0738ee1b9024e5ab5bbaFariborz Jahanian          << (SetterMethod != 0);
156277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        // fixit stuff.
156377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        if (!AttributesAsWritten) {
156477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          if (Property->getLParenLoc().isValid()) {
156577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            // @property () ... case.
156677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceRange PropSourceRange(Property->getAtLoc(),
156777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                        Property->getLParenLoc());
156877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
156977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
157077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          }
157177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          else {
157277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            //@property id etc.
157377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceLocation endLoc =
157477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
157577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            endLoc = endLoc.getLocWithOffset(-1);
157677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
157777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
157877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
157977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          }
158077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        }
158177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
158277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          // @property () ... case.
158377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          SourceLocation endLoc = Property->getLParenLoc();
158477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
158577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
158677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian           FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
158777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        }
158877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        else
158977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
15909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Diag(Property->getLocation(), diag::note_property_declare);
15919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
15929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
15939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
15949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
15959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1596f85e193739c953358c865005855253af4f68a497John McCallvoid Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
15974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::GCOnly)
1598f85e193739c953358c865005855253af4f68a497John McCall    return;
1599f85e193739c953358c865005855253af4f68a497John McCall
1600f85e193739c953358c865005855253af4f68a497John McCall  for (ObjCImplementationDecl::propimpl_iterator
1601f85e193739c953358c865005855253af4f68a497John McCall         i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1602262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyImplDecl *PID = &*i;
1603f85e193739c953358c865005855253af4f68a497John McCall    if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1604f85e193739c953358c865005855253af4f68a497John McCall      continue;
1605f85e193739c953358c865005855253af4f68a497John McCall
1606f85e193739c953358c865005855253af4f68a497John McCall    const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1607831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1608831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian        !D->getInstanceMethod(PD->getGetterName())) {
1609f85e193739c953358c865005855253af4f68a497John McCall      ObjCMethodDecl *method = PD->getGetterMethodDecl();
1610f85e193739c953358c865005855253af4f68a497John McCall      if (!method)
1611f85e193739c953358c865005855253af4f68a497John McCall        continue;
1612f85e193739c953358c865005855253af4f68a497John McCall      ObjCMethodFamily family = method->getMethodFamily();
1613f85e193739c953358c865005855253af4f68a497John McCall      if (family == OMF_alloc || family == OMF_copy ||
1614f85e193739c953358c865005855253af4f68a497John McCall          family == OMF_mutableCopy || family == OMF_new) {
16154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().ObjCAutoRefCount)
1616f85e193739c953358c865005855253af4f68a497John McCall          Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1617f85e193739c953358c865005855253af4f68a497John McCall        else
1618920c9c1155c118ac40e5209d4b757b9f54186339Ted Kremenek          Diag(PID->getLocation(), diag::warn_owning_getter_rule);
1619f85e193739c953358c865005855253af4f68a497John McCall        Diag(PD->getLocation(), diag::note_property_declare);
1620f85e193739c953358c865005855253af4f68a497John McCall      }
1621f85e193739c953358c865005855253af4f68a497John McCall    }
1622f85e193739c953358c865005855253af4f68a497John McCall  }
1623f85e193739c953358c865005855253af4f68a497John McCall}
1624f85e193739c953358c865005855253af4f68a497John McCall
16255de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall/// AddPropertyAttrs - Propagates attributes from a property to the
16265de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall/// implicitly-declared getter or setter for that property.
16275de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCallstatic void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
16285de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall                             ObjCPropertyDecl *Property) {
16295de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall  // Should we just clone all attributes over?
16300a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (Decl::attr_iterator A = Property->attr_begin(),
16310a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                        AEnd = Property->attr_end();
16320a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor       A != AEnd; ++A) {
16330a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isa<DeprecatedAttr>(*A) ||
16340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        isa<UnavailableAttr>(*A) ||
16350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        isa<AvailabilityAttr>(*A))
16360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      PropertyMethod->addAttr((*A)->clone(S.Context));
16370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
16385de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall}
16395de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
16409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
16419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// have the property type and issue diagnostics if they don't.
16429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// Also synthesize a getter/setter method if none exist (and update the
16439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
16449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// methods is the "right" thing to do.
16459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
16468254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCContainerDecl *CD,
16478254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCPropertyDecl *redeclaredProperty,
16488254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCContainerDecl *lexicalDC) {
16498254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
16509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCMethodDecl *GetterMethod, *SetterMethod;
16519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  GetterMethod = CD->getInstanceMethod(property->getGetterName());
16539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  SetterMethod = CD->getInstanceMethod(property->getSetterName());
16549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  DiagnosePropertyAccessorMismatch(property, GetterMethod,
16559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                   property->getLocation());
16569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (SetterMethod) {
16589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl::PropertyAttributeKind CAttr =
16599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      property->getPropertyAttributes();
16609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
16619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Context.getCanonicalType(SetterMethod->getResultType()) !=
16629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          Context.VoidTy)
16639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
16649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (SetterMethod->param_size() != 1 ||
16652aac0c9f8bc4dca3f883f429af8c1f57132d62d0Fariborz Jahanian        !Context.hasSameUnqualifiedType(
1666bb13c320fff4bc4b3536e62626c97d7b055c6113Fariborz Jahanian          (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1667bb13c320fff4bc4b3536e62626c97d7b055c6113Fariborz Jahanian          property->getType().getNonReferenceType())) {
16689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(property->getLocation(),
16699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_accessor_property_type_mismatch)
16709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        << property->getDeclName()
16719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        << SetterMethod->getSelector();
16729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(SetterMethod->getLocation(), diag::note_declared_at);
16739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
16749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
16759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Synthesize getter/setter methods if none exist.
16779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Find the default getter and if one not found, add one.
16789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: The synthesized property we set here is misleading. We almost always
16799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // synthesize these methods unless the user explicitly provided prototypes
16809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // (which is odd, but allowed). Sema should be typechecking that the
16819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // declarations jive in that situation (which it is not currently).
16829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!GetterMethod) {
16839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // No instance method of same name as property getter name was found.
16849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Declare a getter method and add it to the list of methods
16859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // for this class.
1686a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    SourceLocation Loc = redeclaredProperty ?
1687a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek      redeclaredProperty->getLocation() :
1688a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek      property->getLocation();
1689a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek
1690a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1691a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek                             property->getGetterName(),
169275cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             property->getType(), 0, CD, /*isInstance=*/true,
169375cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             /*isVariadic=*/false, /*isSynthesized=*/true,
169475cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
16959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             (property->getPropertyImplementation() ==
16969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                              ObjCPropertyDecl::Optional) ?
16979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             ObjCMethodDecl::Optional :
16989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             ObjCMethodDecl::Required);
16999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    CD->addDecl(GetterMethod);
17005de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17015de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall    AddPropertyAttrs(*this, GetterMethod, property);
17025de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
170323173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek    // FIXME: Eventually this shouldn't be needed, as the lexical context
170423173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek    // and the real context should be the same.
1705a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    if (lexicalDC)
170623173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      GetterMethod->setLexicalDeclContext(lexicalDC);
1707831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    if (property->hasAttr<NSReturnsNotRetainedAttr>())
1708831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian      GetterMethod->addAttr(
1709831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian        ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
17109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  } else
17119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // A user declared getter will be synthesize when @synthesize of
17129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // the property with the same name is seen in the @implementation
17139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    GetterMethod->setSynthesized(true);
17149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  property->setGetterMethodDecl(GetterMethod);
17159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
17169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Skip setter if property is read-only.
17179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!property->isReadOnly()) {
17189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Find the default setter and if one not found, add one.
17199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!SetterMethod) {
17209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // No instance method of same name as property setter name was found.
17219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Declare a setter method and add it to the list of methods
17229d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // for this class.
17238254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      SourceLocation Loc = redeclaredProperty ?
17248254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek        redeclaredProperty->getLocation() :
17258254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek        property->getLocation();
17268254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
17278254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      SetterMethod =
1728491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis        ObjCMethodDecl::Create(Context, Loc, Loc,
17298254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               property->getSetterName(), Context.VoidTy, 0,
173075cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               CD, /*isInstance=*/true, /*isVariadic=*/false,
173175cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isSynthesized=*/true,
173275cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isImplicitlyDeclared=*/true,
173375cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isDefined=*/false,
17349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                               (property->getPropertyImplementation() ==
17359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                ObjCPropertyDecl::Optional) ?
17368254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                                ObjCMethodDecl::Optional :
17378254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                                ObjCMethodDecl::Required);
17388254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
17399d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Invent the arguments for the setter. We don't bother making a
17409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // nice name for the argument.
1741ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1742ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                  Loc, Loc,
17439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  property->getIdentifier(),
1744f85e193739c953358c865005855253af4f68a497John McCall                                    property->getType().getUnqualifiedType(),
17459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  /*TInfo=*/0,
1746d931b086984257de68868a64a235c2b4b34003fbJohn McCall                                                  SC_None,
1747d931b086984257de68868a64a235c2b4b34003fbJohn McCall                                                  SC_None,
17489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  0);
1749491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis      SetterMethod->setMethodParams(Context, Argument,
1750491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis                                    ArrayRef<SourceLocation>());
17515de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17525de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall      AddPropertyAttrs(*this, SetterMethod, property);
17535de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      CD->addDecl(SetterMethod);
175523173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      // FIXME: Eventually this shouldn't be needed, as the lexical context
175623173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      // and the real context should be the same.
17578254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      if (lexicalDC)
175823173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek        SetterMethod->setLexicalDeclContext(lexicalDC);
17599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else
17609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // A user declared setter will be synthesize when @synthesize of
17619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // the property with the same name is seen in the @implementation
17629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      SetterMethod->setSynthesized(true);
17639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    property->setSetterMethodDecl(SetterMethod);
17649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
17659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Add any synthesized methods to the global pool. This allows us to
17669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // handle the following, which is supported by GCC (and part of the design).
17679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @interface Foo
17699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @property double bar;
17709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @end
17719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // void thisIsUnfortunate() {
17739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //   id foo;
17749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //   double bar = [foo bar];
17759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // }
17769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (GetterMethod)
17789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    AddInstanceMethodToGlobalPool(GetterMethod);
17799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (SetterMethod)
17809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    AddInstanceMethodToGlobalPool(SetterMethod);
1781e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis
1782e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1783e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (!CurrentClass) {
1784e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1785e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis      CurrentClass = Cat->getClassInterface();
1786e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1787e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis      CurrentClass = Impl->getClassInterface();
1788e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  }
1789e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (GetterMethod)
1790e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1791e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (SetterMethod)
1792e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
17939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
17949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1795d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Sema::CheckObjCPropertyAttributes(Decl *PDecl,
17969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       SourceLocation Loc,
17979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       unsigned &Attributes) {
17989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: Improve the reported location.
1799f85e193739c953358c865005855253af4f68a497John McCall  if (!PDecl || PDecl->isInvalidDecl())
18005fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek    return;
18015fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek
18025fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek  ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
1803842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian  QualType PropertyTy = PropertyDecl->getType();
18049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
18054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount &&
1806015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1807015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      PropertyTy->isObjCRetainableType()) {
1808015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // 'readonly' property with no obvious lifetime.
1809015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // its life time will be determined by its backing ivar.
1810015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1811015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_copy |
1812015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_retain |
1813015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_strong |
1814015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_weak |
1815015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_assign);
1816015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    if ((Attributes & rel) == 0)
1817015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      return;
1818015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  }
1819015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian
18209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // readonly and readwrite/assign/retain/copy conflict.
18219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
182228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
182328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                     ObjCDeclSpec::DQ_PR_assign |
1824f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_unsafe_unretained |
182528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                     ObjCDeclSpec::DQ_PR_copy |
1826f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_retain |
1827f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_strong))) {
182828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
182928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "readwrite" :
183028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                         (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
183128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "assign" :
1832f85e193739c953358c865005855253af4f68a497John McCall                         (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1833f85e193739c953358c865005855253af4f68a497John McCall                          "unsafe_unretained" :
183428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                         (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
183528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "copy" : "retain";
18369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
183728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
183828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                 diag::err_objc_property_attr_mutually_exclusive :
183928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                 diag::warn_objc_property_attr_mutually_exclusive)
184028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      << "readonly" << which;
184128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
18429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
184328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Check for copy or retain on non-object types.
1844f85e193739c953358c865005855253af4f68a497John McCall  if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1845f85e193739c953358c865005855253af4f68a497John McCall                    ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1846f85e193739c953358c865005855253af4f68a497John McCall      !PropertyTy->isObjCRetainableType() &&
1847842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian      !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
184828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, diag::err_objc_property_requires_object)
1849f85e193739c953358c865005855253af4f68a497John McCall      << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1850f85e193739c953358c865005855253af4f68a497John McCall          Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1851f85e193739c953358c865005855253af4f68a497John McCall    Attributes &= ~(ObjCDeclSpec::DQ_PR_weak   | ObjCDeclSpec::DQ_PR_copy |
1852f85e193739c953358c865005855253af4f68a497John McCall                    ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
1853977ea7864a50eee39f6bef8a5a79154ac9ba4b3eJohn McCall    PropertyDecl->setInvalidDecl();
185428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
18559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
185628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Check for more than one of { assign, copy, retain }.
185728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
185828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
185928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
186028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "assign" << "copy";
186128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
186228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
186328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
186428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
186528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "assign" << "retain";
186628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
186728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
1868f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1869f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1870f85e193739c953358c865005855253af4f68a497John McCall        << "assign" << "strong";
1871f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1872f85e193739c953358c865005855253af4f68a497John McCall    }
18734e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount  &&
1874f85e193739c953358c865005855253af4f68a497John McCall        (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1875f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1876f85e193739c953358c865005855253af4f68a497John McCall        << "assign" << "weak";
1877f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1878f85e193739c953358c865005855253af4f68a497John McCall    }
1879f85e193739c953358c865005855253af4f68a497John McCall  } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1880f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1881f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1882f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "copy";
1883f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1884f85e193739c953358c865005855253af4f68a497John McCall    }
1885f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1886f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1887f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "retain";
1888f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1889f85e193739c953358c865005855253af4f68a497John McCall    }
1890f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1891f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1892f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "strong";
1893f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1894f85e193739c953358c865005855253af4f68a497John McCall    }
18954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount  &&
1896f85e193739c953358c865005855253af4f68a497John McCall        (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1897f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1898f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "weak";
1899f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1900f85e193739c953358c865005855253af4f68a497John McCall    }
190128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
190228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
190328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
190428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "copy" << "retain";
190528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
190628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
1907f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1908f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1909f85e193739c953358c865005855253af4f68a497John McCall        << "copy" << "strong";
1910f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1911f85e193739c953358c865005855253af4f68a497John McCall    }
1912f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1913f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1914f85e193739c953358c865005855253af4f68a497John McCall        << "copy" << "weak";
1915f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1916f85e193739c953358c865005855253af4f68a497John McCall    }
1917f85e193739c953358c865005855253af4f68a497John McCall  }
1918f85e193739c953358c865005855253af4f68a497John McCall  else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1919f85e193739c953358c865005855253af4f68a497John McCall           (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1920f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1921f85e193739c953358c865005855253af4f68a497John McCall        << "retain" << "weak";
1922528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1923f85e193739c953358c865005855253af4f68a497John McCall  }
1924f85e193739c953358c865005855253af4f68a497John McCall  else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1925f85e193739c953358c865005855253af4f68a497John McCall           (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1926f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1927f85e193739c953358c865005855253af4f68a497John McCall        << "strong" << "weak";
1928f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
192928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
19309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
19319d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian  if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
19329d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
19339d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
19349d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian        << "atomic" << "nonatomic";
19359d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
19369d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian  }
19379d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian
193828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Warn if user supplied no assignment attribute, property is
193928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // readwrite, and this is an object type.
194028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1941f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_unsafe_unretained |
1942f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1943f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_weak)) &&
194428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      PropertyTy->isObjCObjectPointerType()) {
19454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount)
1946bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian        // With arc,  @property definitions should default to (strong) when
1947f21a92d35953adb038ffbe37cd1a4083e0df0ec0Fariborz Jahanian        // not specified; including when property is 'readonly'.
1948bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian        PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1949f21a92d35953adb038ffbe37cd1a4083e0df0ec0Fariborz Jahanian      else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
19509f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        bool isAnyClassTy =
19519f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian          (PropertyTy->isObjCClassType() ||
19529f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian           PropertyTy->isObjCQualifiedClassType());
19539f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
19549f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        // issue any warning.
19554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
19569f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian          ;
19579f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        else {
1958bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian          // Skip this warning in gc-only mode.
19594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (getLangOpts().getGC() != LangOptions::GCOnly)
1960bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian            Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
19610a68dc7f04be1542ce249ff4adb169453698ad91Argyrios Kyrtzidis
1962bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian          // If non-gc code warn that this is likely inappropriate.
19634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (getLangOpts().getGC() == LangOptions::NonGC)
1964bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian            Diag(Loc, diag::warn_objc_property_default_assign_on_object);
19659f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        }
1966bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian      }
19679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
196828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // FIXME: Implement warning dependent on NSCopying being
196928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // implemented. See also:
197028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
197128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // (please trim this list while you are at it).
197228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
19739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
197428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
19752b77cb803c76d01d2d10a58cec9334e2090cd7c3Fariborz Jahanian      &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
19764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      && getLangOpts().getGC() == LangOptions::GCOnly
197728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      && PropertyTy->isBlockPointerType())
197828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
19794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  else if (getLangOpts().ObjCAutoRefCount &&
1980528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1981528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1982528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1983528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           PropertyTy->isBlockPointerType())
1984528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian      Diag(Loc, diag::warn_objc_property_retain_of_block);
198548a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian
198648a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
198748a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_setter))
198848a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian    Diag(Loc, diag::warn_objc_readonly_property_has_setter);
198948a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian
19909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
1991