SemaObjCProperty.cpp revision 9abf88c9286d7465ce18285f87bb302f588b59fc
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) {
10539abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  if (!GetterMethod)
10549abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    return false;
10559abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
10569abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  QualType PropertyIvarType = property->getType().getNonReferenceType();
10579abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  bool compat = Context.hasSameType(PropertyIvarType, GetterType);
10589abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  if (!compat) {
10599abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
10609abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian        isa<ObjCObjectPointerType>(GetterType))
10619abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian      compat =
10629abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian        Context.canAssignObjCInterfaces(
10639abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian                                      PropertyIvarType->getAs<ObjCObjectPointerType>(),
10649abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian                                      GetterType->getAs<ObjCObjectPointerType>());
10659abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    else if (CheckAssignmentConstraints(Loc, PropertyIvarType, GetterType)
10669abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian              != Compatible) {
10679abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian          Diag(Loc, diag::error_property_accessor_type)
10689abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian            << property->getDeclName() << PropertyIvarType
10699abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian            << GetterMethod->getSelector() << GetterType;
10709abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian          Diag(GetterMethod->getLocation(), diag::note_declared_at);
10719abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian          return true;
10729abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    } else {
10739abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian      compat = true;
10749abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian      QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
10759abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian      QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
10769abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian      if (lhsType != rhsType && lhsType->isArithmeticType())
10779abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian        compat = false;
10789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
10799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
10809abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian
10819abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  if (!compat) {
10829abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    Diag(Loc, diag::warn_accessor_property_type_mismatch)
10839abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    << property->getDeclName()
10849abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    << GetterMethod->getSelector();
10859abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    Diag(GetterMethod->getLocation(), diag::note_declared_at);
10869abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian    return true;
10879abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian  }
10889abf88c9286d7465ce18285f87bb302f588b59fcFariborz Jahanian
10899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return false;
10909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
10919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
10929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// ComparePropertiesInBaseAndSuper - This routine compares property
10939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// declarations in base and its super class, if any, and issues
1094fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// diagnostics in a variety of inconsistent situations.
10959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
10969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
10979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
10989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!SDecl)
10999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
11009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: O(N^2)
11019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
11029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = SDecl->prop_end(); S != E; ++S) {
1103262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *SuperPDecl = &*S;
11049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Does property in super class has declaration in current class?
11059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
11069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); I != E; ++I) {
1107262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *PDecl = &*I;
11089d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
11099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          DiagnosePropertyMismatch(PDecl, SuperPDecl,
11109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                   SDecl->getIdentifier());
11119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
11129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
11149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
11169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// of properties declared in a protocol and compares their attribute against
11179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// the same property declared in the class or category.
11189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid
11199d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekSema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
11209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                          ObjCProtocolDecl *PDecl) {
11219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
11229d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!IDecl) {
11239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Category
11249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
11259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    assert (CatDecl && "MatchOneProtocolPropertiesInClass");
11269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!CatDecl->IsClassExtension())
11279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
11289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = PDecl->prop_end(); P != E; ++P) {
1129262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        ObjCPropertyDecl *Pr = &*P;
11309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        ObjCCategoryDecl::prop_iterator CP, CE;
11319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        // Is this property already in  category's list of properties?
11322d2f9368d35b3628c7e3b4563f74849a0f901a00Ted Kremenek        for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
1133262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          if (CP->getIdentifier() == Pr->getIdentifier())
11349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            break;
11359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        if (CP != CE)
11369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          // Property protocol already exist in class. Diagnose any mismatch.
1137262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie          DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
11389d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
11399d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
11409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
11429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = PDecl->prop_end(); P != E; ++P) {
1143262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *Pr = &*P;
11449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCInterfaceDecl::prop_iterator CP, CE;
11459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Is this property already in  class's list of properties?
11469d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1147262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      if (CP->getIdentifier() == Pr->getIdentifier())
11489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        break;
11499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (CP != CE)
11509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Property protocol already exist in class. Diagnose any mismatch.
1151262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier());
11529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
11539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
11549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// CompareProperties - This routine compares properties
11569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// declared in 'ClassOrProtocol' objects (which can be a class or an
11579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// inherited protocol with the list of properties for class/category 'CDecl'
11589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
1159d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1160d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClassDecl = ClassOrProtocol;
11619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
11629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!IDecl) {
11649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Category
11659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
11669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    assert (CatDecl && "CompareProperties");
11679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
11689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
11699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = MDecl->protocol_end(); P != E; ++P)
11709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Match properties of category with those of protocol (*P)
11719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(CatDecl, *P);
11729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Go thru the list of protocols for this category and recursively match
11749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // their properties with those in the category.
11759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
11769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = CatDecl->protocol_end(); P != E; ++P)
1177d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        CompareProperties(CatDecl, *P);
11789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else {
11799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
11809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
11819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = MD->protocol_end(); P != E; ++P)
11829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        MatchOneProtocolPropertiesInClass(CatDecl, *P);
11839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
11849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
11859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
11869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
118853b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
118953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          P = MDecl->all_referenced_protocol_begin(),
119053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          E = MDecl->all_referenced_protocol_end(); P != E; ++P)
11919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Match properties of class IDecl with those of protocol (*P).
11929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(IDecl, *P);
11939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
11949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Go thru the list of protocols for this class and recursively match
11959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // their properties with those declared in the class.
119653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
119753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          P = IDecl->all_referenced_protocol_begin(),
119853b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek          E = IDecl->all_referenced_protocol_end(); P != E; ++P)
1199d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      CompareProperties(IDecl, *P);
12009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  } else {
12019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
12029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
12039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = MD->protocol_end(); P != E; ++P)
12049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      MatchOneProtocolPropertiesInClass(IDecl, *P);
12059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
12079d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12089d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// isPropertyReadonly - Return true if property is readonly, by searching
12099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// for the property in the class and in its categories and implementations
12109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek///
12119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekbool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
12129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                              ObjCInterfaceDecl *IDecl) {
12139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // by far the most common case.
12149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!PDecl->isReadOnly())
12159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return false;
12169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Even if property is ready only, if interface has a user defined setter,
12179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // it is not considered read only.
12189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (IDecl->getInstanceMethod(PDecl->getSetterName()))
12199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return false;
12209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12219d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Main class has the property as 'readonly'. Must search
12229d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // through the category list to see if the property's
12239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // attribute has been over-ridden to 'readwrite'.
12249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
12259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       Category; Category = Category->getNextClassCategory()) {
12269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Even if property is ready only, if a category has a user defined setter,
12279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // it is not considered read only.
12289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (Category->getInstanceMethod(PDecl->getSetterName()))
12299d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl *P =
12319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Category->FindPropertyDeclaration(PDecl->getIdentifier());
12329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (P && !P->isReadOnly())
12339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Also, check for definition of a setter method in the implementation if
12379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // all else failed.
12389d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
12399d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ObjCImplementationDecl *IMD =
12409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
12419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (IMD->getInstanceMethod(PDecl->getSetterName()))
12429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return false;
12439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else if (ObjCCategoryImplDecl *CIMD =
12449d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek               dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
12459d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (CIMD->getInstanceMethod(PDecl->getSetterName()))
12469d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return false;
12479d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
12489d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12499d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Lastly, look through the implementation (if one is in scope).
12509d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
12519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
12529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      return false;
12539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // If all fails, look at the super class.
12549d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
12559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return isPropertyReadonly(PDecl, SIDecl);
12569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return true;
12579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
12589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
12599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// CollectImmediateProperties - This routine collects all properties in
12609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// the class and its conforming protocols; but not those it its super class.
12619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
1262cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian            llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1263cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian            llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
12649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
12659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
12669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); P != E; ++P) {
1267262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
12689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      PropMap[Prop->getIdentifier()] = Prop;
12699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
12709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
127153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
127253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
127353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1274cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        CollectImmediateProperties((*PI), PropMap, SuperPropMap);
12759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
12779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!CATDecl->IsClassExtension())
12789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
12799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           E = CATDecl->prop_end(); P != E; ++P) {
1280262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        ObjCPropertyDecl *Prop = &*P;
12819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        PropMap[Prop->getIdentifier()] = Prop;
12829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
12839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
128453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
12859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = CATDecl->protocol_end(); PI != E; ++PI)
1286cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      CollectImmediateProperties((*PI), PropMap, SuperPropMap);
12879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
12889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
12899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
12909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->prop_end(); P != E; ++P) {
1291262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1292cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1293cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      // Exclude property for protocols which conform to class's super-class,
1294cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      // as super-class has to implement the property.
1295a929ec7b673b244bec024ba7eb95140404a7af5dFariborz Jahanian      if (!PropertyFromSuper ||
1296a929ec7b673b244bec024ba7eb95140404a7af5dFariborz Jahanian          PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
1297cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1298cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian        if (!PropEntry)
1299cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian          PropEntry = Prop;
1300cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      }
13019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through protocol's protocols.
13039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
13049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->protocol_end(); PI != E; ++PI)
1305cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian      CollectImmediateProperties((*PI), PropMap, SuperPropMap);
13069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
13079d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
13089d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1309509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// CollectClassPropertyImplementations - This routine collects list of
1310509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties to be implemented in the class. This includes, class's
1311509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// and its conforming protocols' properties.
1312509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanianstatic void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1313509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian                llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1314509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1315509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1316509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = IDecl->prop_end(); P != E; ++P) {
1317262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1318509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      PropMap[Prop->getIdentifier()] = Prop;
1319509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
132053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
132153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
132253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
1323509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations((*PI), PropMap);
1324509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1325509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1326509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1327509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = PDecl->prop_end(); P != E; ++P) {
1328262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
1329ac37150c13c4e6f54551c52a1118f105be5f136bFariborz Jahanian      if (!PropMap.count(Prop->getIdentifier()))
1330ac37150c13c4e6f54551c52a1118f105be5f136bFariborz Jahanian        PropMap[Prop->getIdentifier()] = Prop;
1331509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
1332509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // scan through protocol's protocols.
1333509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1334509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian         E = PDecl->protocol_end(); PI != E; ++PI)
1335509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations((*PI), PropMap);
1336509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1337509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
1338509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1339509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// CollectSuperClassPropertyImplementations - This routine collects list of
1340509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties to be implemented in super class(s) and also coming from their
1341509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// conforming protocols.
1342509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanianstatic void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1343509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian                llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1344509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1345509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    while (SDecl) {
1346509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      CollectClassPropertyImplementations(SDecl, PropMap);
1347509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      SDecl = SDecl->getSuperClass();
1348509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    }
1349509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  }
1350509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
1351509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
13529d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// LookupPropertyDecl - Looks up a property in the current class and all
13539d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// its protocols.
13549d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
13559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                     IdentifierInfo *II) {
13569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (const ObjCInterfaceDecl *IDecl =
13579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        dyn_cast<ObjCInterfaceDecl>(CDecl)) {
13589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
13599d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = IDecl->prop_end(); P != E; ++P) {
1360262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
13619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop->getIdentifier() == II)
13629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through class's protocols.
136553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator
136653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PI = IDecl->all_referenced_protocol_begin(),
136753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
13689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
13699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop)
13709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
13739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  else if (const ObjCProtocolDecl *PDecl =
13749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            dyn_cast<ObjCProtocolDecl>(CDecl)) {
13759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
13769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->prop_end(); P != E; ++P) {
1377262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      ObjCPropertyDecl *Prop = &*P;
13789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop->getIdentifier() == II)
13799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // scan through protocol's protocols.
13829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
13839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         E = PDecl->protocol_end(); PI != E; ++PI) {
13849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
13859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (Prop)
13869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        return Prop;
13879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
13889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
13899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  return 0;
13909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
13919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1392d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenekstatic IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1393d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek                                                ASTContext &Ctx) {
1394f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<128> ivarName;
1395d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  {
1396d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek    llvm::raw_svector_ostream os(ivarName);
1397d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek    os << '_' << Prop->getIdentifier()->getName();
1398d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  }
1399d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek  return &Ctx.Idents.get(ivarName.str());
1400d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek}
1401d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek
1402509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// DefaultSynthesizeProperties - This routine default synthesizes all
1403509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian/// properties which must be synthesized in class's @implementation.
1404d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenekvoid Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1405d2ee809f9e7c2c7a83b8918efa11e98863463f78Ted Kremenek                                       ObjCInterfaceDecl *IDecl) {
1406509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1407509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1408509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  CollectClassPropertyImplementations(IDecl, PropMap);
1409509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  if (PropMap.empty())
1410509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    return;
1411509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1412509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1413509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian
1414509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian  for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1415509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian       P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1416509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    ObjCPropertyDecl *Prop = P->second;
1417509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // If property to be implemented in the super class, ignore.
1418509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    if (SuperPropMap[Prop->getIdentifier()])
1419509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      continue;
1420509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    // Is there a matching propery synthesize/dynamic?
1421509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian    if (Prop->isInvalidDecl() ||
1422509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1423509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian        IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1424509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian      continue;
1425d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian    // Property may have been synthesized by user.
1426d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian    if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1427d3635b9681daaef74974932f77080910b032b9fdFariborz Jahanian      continue;
142895f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian    if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
142995f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian      if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
143095f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian        continue;
143195f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian      if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
143295f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian        continue;
143395f1b86b73a9a203311adc7646f2fa9ee5cdd2b7Fariborz Jahanian    }
1434f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian    if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1435f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      // We won't auto-synthesize properties declared in protocols.
1436f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      Diag(IMPDecl->getLocation(),
1437f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian           diag::warn_auto_synthesizing_protocol_property);
1438f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      Diag(Prop->getLocation(), diag::note_property_declare);
1439f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian      continue;
1440f8aba8c618920db8f6ef2db0c554b0c270503cefFariborz Jahanian    }
14412a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek
14422a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // We use invalid SourceLocations for the synthesized ivars since they
14432a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // aren't really synthesized at a particular location; they just exist.
14442a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // Saying that they are located at the @implementation isn't really going
14452a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek    // to help users.
1446975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1447975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian      ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1448975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            true,
1449975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            /* property = */ Prop->getIdentifier(),
1450975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            /* ivar = */ getDefaultSynthIvarName(Prop, Context),
1451975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian                            SourceLocation()));
1452975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    if (PIDecl) {
1453975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian      Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
14545ea66619a3108e2985e952b9a60a84316d464b25Fariborz Jahanian      Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1455975eef64182a78d1afc0f3da8cba4cb961cda5e4Fariborz Jahanian    }
14562a6af6b7874e41f3e6213e8d18623c4bac150354Ted Kremenek  }
1457509d477ae5a24282719320fe92038ccf57358840Fariborz Jahanian}
14589d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14598697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanianvoid Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
14608697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
14618697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian    return;
14628697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
14638697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (!IC)
14648697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian    return;
14658697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
146671207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek    if (!IDecl->isObjCRequiresPropertyDefs())
1467eb4f2c56c298071d58b441ccf801b039be93788aFariborz Jahanian      DefaultSynthesizeProperties(S, IC, IDecl);
14688697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian}
14698697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
147017cb326cb62a59f53d92236394af40eaae4eddbdFariborz Jahanianvoid Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
14719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                      ObjCContainerDecl *CDecl,
14729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                      const llvm::DenseSet<Selector>& InsMap) {
1473cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1474cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1475cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian    CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1476cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian
14779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1478cfa6a27f3cc5add888c6ac84dbcc45854cfd8666Fariborz Jahanian  CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
14799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (PropMap.empty())
14809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
14819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
14839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCImplDecl::propimpl_iterator
14849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       I = IMPDecl->propimpl_begin(),
14859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       EI = IMPDecl->propimpl_end(); I != EI; ++I)
1486262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    PropImplMap.insert(I->getPropertyDecl());
14879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
14889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
14899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
14909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl *Prop = P->second;
14919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Is there a matching propery synthesize/dynamic?
14929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (Prop->isInvalidDecl() ||
14939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1494327126ee3d2faad9314b2633974eefc672f73b79Fariborz Jahanian        PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
14959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      continue;
14969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!InsMap.count(Prop->getGetterName())) {
1497b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(IMPDecl->getLocation(),
14989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           isa<ObjCCategoryDecl>(CDecl) ?
14999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            diag::warn_setter_getter_impl_required_in_category :
15009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek            diag::warn_setter_getter_impl_required)
15019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Prop->getDeclName() << Prop->getGetterName();
1502b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(Prop->getLocation(),
1503b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian           diag::note_property_declare);
1504da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian      if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1505da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian        if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
150671207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek          if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1507da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian            Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1508da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian
15099d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
15109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
15119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1512b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(IMPDecl->getLocation(),
15139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           isa<ObjCCategoryDecl>(CDecl) ?
15149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_setter_getter_impl_required_in_category :
15159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_setter_getter_impl_required)
15169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      << Prop->getDeclName() << Prop->getSetterName();
1517b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian      Diag(Prop->getLocation(),
1518b8607398a5e1e552f82a86d1d8c3a4031ac4c946Fariborz Jahanian           diag::note_property_declare);
1519da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian      if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)
1520da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian        if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
152171207fc0470e1eee40a2951cd5cc3ff47725b755Ted Kremenek          if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1522da611a74c408af0f9526acc690b85214bf180852Fariborz Jahanian            Diag(RID->getLocation(), diag::note_suppressed_class_declare);
15239d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
15249d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
15259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
15269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
15279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid
15289d64c15223497f31899cc47fbe1531873dc06f2eTed KremenekSema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
15299d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       ObjCContainerDecl* IDecl) {
15309d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Rules apply in non-GC mode only
15314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() != LangOptions::NonGC)
15329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    return;
15339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
15349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       E = IDecl->prop_end();
15359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek       I != E; ++I) {
1536262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyDecl *Property = &*I;
153794659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    ObjCMethodDecl *GetterMethod = 0;
153894659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    ObjCMethodDecl *SetterMethod = 0;
153994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    bool LookedUpGetterSetter = false;
154094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
15419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    unsigned Attributes = Property->getPropertyAttributes();
1542265941bc308d65cc270d5c4de5806f37ce405606John McCall    unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
154394659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
1544265941bc308d65cc270d5c4de5806f37ce405606John McCall    if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1545265941bc308d65cc270d5c4de5806f37ce405606John McCall        !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
154694659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
154794659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
154894659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      LookedUpGetterSetter = true;
154994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (GetterMethod) {
155094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(GetterMethod->getLocation(),
155194659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis             diag::warn_default_atomic_custom_getter_setter)
1552293a45e724a15fb58b8805a5791f9f3aee769cf6Argyrios Kyrtzidis          << Property->getIdentifier() << 0;
155394659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(Property->getLocation(), diag::note_property_declare);
155494659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
155594659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (SetterMethod) {
155694659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(SetterMethod->getLocation(),
155794659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis             diag::warn_default_atomic_custom_getter_setter)
1558293a45e724a15fb58b8805a5791f9f3aee769cf6Argyrios Kyrtzidis          << Property->getIdentifier() << 1;
155994659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        Diag(Property->getLocation(), diag::note_property_declare);
156094659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
156194659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis    }
156294659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis
15639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // We only care about readwrite atomic property.
15649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
15659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
15669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      continue;
15679d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (const ObjCPropertyImplDecl *PIDecl
15689d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek         = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
15699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
15709d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        continue;
157194659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      if (!LookedUpGetterSetter) {
157294659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
157394659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
157494659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis        LookedUpGetterSetter = true;
157594659e4bdb87534f591ae185812548c42d6efacbArgyrios Kyrtzidis      }
15769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
15779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        SourceLocation MethodLoc =
15789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          (GetterMethod ? GetterMethod->getLocation()
15799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                        : SetterMethod->getLocation());
15809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Diag(MethodLoc, diag::warn_atomic_property_rule)
15817d65f6965d215f4cb2eb0738ee1b9024e5ab5bbaFariborz Jahanian          << Property->getIdentifier() << (GetterMethod != 0)
15827d65f6965d215f4cb2eb0738ee1b9024e5ab5bbaFariborz Jahanian          << (SetterMethod != 0);
158377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        // fixit stuff.
158477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        if (!AttributesAsWritten) {
158577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          if (Property->getLParenLoc().isValid()) {
158677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            // @property () ... case.
158777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceRange PropSourceRange(Property->getAtLoc(),
158877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                        Property->getLParenLoc());
158977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
159077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
159177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          }
159277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          else {
159377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            //@property id etc.
159477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceLocation endLoc =
159577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
159677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            endLoc = endLoc.getLocWithOffset(-1);
159777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
159877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian            Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
159977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian              FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
160077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          }
160177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        }
160277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
160377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          // @property () ... case.
160477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          SourceLocation endLoc = Property->getLParenLoc();
160577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
160677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
160777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian           FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
160877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        }
160977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        else
161077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian          Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
16119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Diag(Property->getLocation(), diag::note_property_declare);
16129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      }
16139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
16149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
16159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
16169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1617f85e193739c953358c865005855253af4f68a497John McCallvoid Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
16184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::GCOnly)
1619f85e193739c953358c865005855253af4f68a497John McCall    return;
1620f85e193739c953358c865005855253af4f68a497John McCall
1621f85e193739c953358c865005855253af4f68a497John McCall  for (ObjCImplementationDecl::propimpl_iterator
1622f85e193739c953358c865005855253af4f68a497John McCall         i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1623262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    ObjCPropertyImplDecl *PID = &*i;
1624f85e193739c953358c865005855253af4f68a497John McCall    if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1625f85e193739c953358c865005855253af4f68a497John McCall      continue;
1626f85e193739c953358c865005855253af4f68a497John McCall
1627f85e193739c953358c865005855253af4f68a497John McCall    const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1628831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1629831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian        !D->getInstanceMethod(PD->getGetterName())) {
1630f85e193739c953358c865005855253af4f68a497John McCall      ObjCMethodDecl *method = PD->getGetterMethodDecl();
1631f85e193739c953358c865005855253af4f68a497John McCall      if (!method)
1632f85e193739c953358c865005855253af4f68a497John McCall        continue;
1633f85e193739c953358c865005855253af4f68a497John McCall      ObjCMethodFamily family = method->getMethodFamily();
1634f85e193739c953358c865005855253af4f68a497John McCall      if (family == OMF_alloc || family == OMF_copy ||
1635f85e193739c953358c865005855253af4f68a497John McCall          family == OMF_mutableCopy || family == OMF_new) {
16364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (getLangOpts().ObjCAutoRefCount)
1637f85e193739c953358c865005855253af4f68a497John McCall          Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1638f85e193739c953358c865005855253af4f68a497John McCall        else
1639920c9c1155c118ac40e5209d4b757b9f54186339Ted Kremenek          Diag(PID->getLocation(), diag::warn_owning_getter_rule);
1640f85e193739c953358c865005855253af4f68a497John McCall        Diag(PD->getLocation(), diag::note_property_declare);
1641f85e193739c953358c865005855253af4f68a497John McCall      }
1642f85e193739c953358c865005855253af4f68a497John McCall    }
1643f85e193739c953358c865005855253af4f68a497John McCall  }
1644f85e193739c953358c865005855253af4f68a497John McCall}
1645f85e193739c953358c865005855253af4f68a497John McCall
16465de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall/// AddPropertyAttrs - Propagates attributes from a property to the
16475de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall/// implicitly-declared getter or setter for that property.
16485de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCallstatic void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
16495de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall                             ObjCPropertyDecl *Property) {
16505de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall  // Should we just clone all attributes over?
16510a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  for (Decl::attr_iterator A = Property->attr_begin(),
16520a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                        AEnd = Property->attr_end();
16530a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor       A != AEnd; ++A) {
16540a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isa<DeprecatedAttr>(*A) ||
16550a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        isa<UnavailableAttr>(*A) ||
16560a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        isa<AvailabilityAttr>(*A))
16570a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      PropertyMethod->addAttr((*A)->clone(S.Context));
16580a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
16595de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall}
16605de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
16619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
16629d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// have the property type and issue diagnostics if they don't.
16639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// Also synthesize a getter/setter method if none exist (and update the
16649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
16659d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/// methods is the "right" thing to do.
16669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenekvoid Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
16678254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCContainerDecl *CD,
16688254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCPropertyDecl *redeclaredProperty,
16698254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               ObjCContainerDecl *lexicalDC) {
16708254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
16719d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  ObjCMethodDecl *GetterMethod, *SetterMethod;
16729d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16739d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  GetterMethod = CD->getInstanceMethod(property->getGetterName());
16749d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  SetterMethod = CD->getInstanceMethod(property->getSetterName());
16759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  DiagnosePropertyAccessorMismatch(property, GetterMethod,
16769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                   property->getLocation());
16779d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16789d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (SetterMethod) {
16799d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    ObjCPropertyDecl::PropertyAttributeKind CAttr =
16809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      property->getPropertyAttributes();
16819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
16829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        Context.getCanonicalType(SetterMethod->getResultType()) !=
16839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek          Context.VoidTy)
16849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
16859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (SetterMethod->param_size() != 1 ||
16862aac0c9f8bc4dca3f883f429af8c1f57132d62d0Fariborz Jahanian        !Context.hasSameUnqualifiedType(
1687bb13c320fff4bc4b3536e62626c97d7b055c6113Fariborz Jahanian          (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1688bb13c320fff4bc4b3536e62626c97d7b055c6113Fariborz Jahanian          property->getType().getNonReferenceType())) {
16899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(property->getLocation(),
16909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek           diag::warn_accessor_property_type_mismatch)
16919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        << property->getDeclName()
16929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek        << SetterMethod->getSelector();
16939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      Diag(SetterMethod->getLocation(), diag::note_declared_at);
16949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    }
16959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
16969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
16979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Synthesize getter/setter methods if none exist.
16989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Find the default getter and if one not found, add one.
16999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: The synthesized property we set here is misleading. We almost always
17009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // synthesize these methods unless the user explicitly provided prototypes
17019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // (which is odd, but allowed). Sema should be typechecking that the
17029d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // declarations jive in that situation (which it is not currently).
17039d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!GetterMethod) {
17049d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // No instance method of same name as property getter name was found.
17059d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Declare a getter method and add it to the list of methods
17069d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // for this class.
1707a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    SourceLocation Loc = redeclaredProperty ?
1708a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek      redeclaredProperty->getLocation() :
1709a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek      property->getLocation();
1710a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek
1711a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1712a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek                             property->getGetterName(),
171375cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             property->getType(), 0, CD, /*isInstance=*/true,
171475cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             /*isVariadic=*/false, /*isSynthesized=*/true,
171575cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
17169d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             (property->getPropertyImplementation() ==
17179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                              ObjCPropertyDecl::Optional) ?
17189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             ObjCMethodDecl::Optional :
17199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                             ObjCMethodDecl::Required);
17209d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    CD->addDecl(GetterMethod);
17215de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17225de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall    AddPropertyAttrs(*this, GetterMethod, property);
17235de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
172423173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek    // FIXME: Eventually this shouldn't be needed, as the lexical context
172523173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek    // and the real context should be the same.
1726a054fb46b1fb596d1719b89d2d9a5be3c32a4b0dTed Kremenek    if (lexicalDC)
172723173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      GetterMethod->setLexicalDeclContext(lexicalDC);
1728831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian    if (property->hasAttr<NSReturnsNotRetainedAttr>())
1729831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian      GetterMethod->addAttr(
1730831fb9622581fc3b777848e6b097a0cb23d124deFariborz Jahanian        ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
17319d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  } else
17329d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // A user declared getter will be synthesize when @synthesize of
17339d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // the property with the same name is seen in the @implementation
17349d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    GetterMethod->setSynthesized(true);
17359d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  property->setGetterMethodDecl(GetterMethod);
17369d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
17379d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Skip setter if property is read-only.
17389d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (!property->isReadOnly()) {
17399d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    // Find the default setter and if one not found, add one.
17409d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    if (!SetterMethod) {
17419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // No instance method of same name as property setter name was found.
17429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Declare a setter method and add it to the list of methods
17439d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // for this class.
17448254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      SourceLocation Loc = redeclaredProperty ?
17458254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek        redeclaredProperty->getLocation() :
17468254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek        property->getLocation();
17478254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
17488254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      SetterMethod =
1749491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis        ObjCMethodDecl::Create(Context, Loc, Loc,
17508254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                               property->getSetterName(), Context.VoidTy, 0,
175175cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               CD, /*isInstance=*/true, /*isVariadic=*/false,
175275cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isSynthesized=*/true,
175375cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isImplicitlyDeclared=*/true,
175475cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                               /*isDefined=*/false,
17559d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                               (property->getPropertyImplementation() ==
17569d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                ObjCPropertyDecl::Optional) ?
17578254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                                ObjCMethodDecl::Optional :
17588254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek                                ObjCMethodDecl::Required);
17598254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek
17609d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // Invent the arguments for the setter. We don't bother making a
17619d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // nice name for the argument.
1762ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1763ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                  Loc, Loc,
17649d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  property->getIdentifier(),
1765f85e193739c953358c865005855253af4f68a497John McCall                                    property->getType().getUnqualifiedType(),
17669d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  /*TInfo=*/0,
1767d931b086984257de68868a64a235c2b4b34003fbJohn McCall                                                  SC_None,
1768d931b086984257de68868a64a235c2b4b34003fbJohn McCall                                                  SC_None,
17699d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                                  0);
1770491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis      SetterMethod->setMethodParams(Context, Argument,
1771491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis                                    ArrayRef<SourceLocation>());
17725de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17735de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall      AddPropertyAttrs(*this, SetterMethod, property);
17745de74d19ddcf84cfe9d7d973192ba93fd0c64d4fJohn McCall
17759d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      CD->addDecl(SetterMethod);
177623173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      // FIXME: Eventually this shouldn't be needed, as the lexical context
177723173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek      // and the real context should be the same.
17788254aa62d9189395be1eed61194cd4b5ee6cb4a2Ted Kremenek      if (lexicalDC)
177923173d7f029f430611caceea72ae61ba6b80af1cTed Kremenek        SetterMethod->setLexicalDeclContext(lexicalDC);
17809d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    } else
17819d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // A user declared setter will be synthesize when @synthesize of
17829d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      // the property with the same name is seen in the @implementation
17839d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek      SetterMethod->setSynthesized(true);
17849d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    property->setSetterMethodDecl(SetterMethod);
17859d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  }
17869d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // Add any synthesized methods to the global pool. This allows us to
17879d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // handle the following, which is supported by GCC (and part of the design).
17889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17899d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @interface Foo
17909d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @property double bar;
17919d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // @end
17929d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17939d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // void thisIsUnfortunate() {
17949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //   id foo;
17959d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //   double bar = [foo bar];
17969d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // }
17979d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  //
17989d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (GetterMethod)
17999d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    AddInstanceMethodToGlobalPool(GetterMethod);
18009d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if (SetterMethod)
18019d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek    AddInstanceMethodToGlobalPool(SetterMethod);
1802e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis
1803e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1804e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (!CurrentClass) {
1805e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1806e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis      CurrentClass = Cat->getClassInterface();
1807e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1808e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis      CurrentClass = Impl->getClassInterface();
1809e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  }
1810e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (GetterMethod)
1811e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1812e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis  if (SetterMethod)
1813e15db6f0d226a3bc88d244512d1004c7c1c07391Argyrios Kyrtzidis    CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
18149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
18159d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
1816d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Sema::CheckObjCPropertyAttributes(Decl *PDecl,
18179d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       SourceLocation Loc,
18189d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek                                       unsigned &Attributes) {
18199d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // FIXME: Improve the reported location.
1820f85e193739c953358c865005855253af4f68a497John McCall  if (!PDecl || PDecl->isInvalidDecl())
18215fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek    return;
18225fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek
18235fcd52a6c94c7f93bd80062248353c45330da23aTed Kremenek  ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
1824842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian  QualType PropertyTy = PropertyDecl->getType();
18259d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
18264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjCAutoRefCount &&
1827015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1828015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      PropertyTy->isObjCRetainableType()) {
1829015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // 'readonly' property with no obvious lifetime.
1830015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    // its life time will be determined by its backing ivar.
1831015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1832015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_copy |
1833015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_retain |
1834015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_strong |
1835015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_weak |
1836015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian                    ObjCDeclSpec::DQ_PR_assign);
1837015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian    if ((Attributes & rel) == 0)
1838015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian      return;
1839015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian  }
1840015f608042a1d247899866849e69a4e9a1aff6a8Fariborz Jahanian
18419d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  // readonly and readwrite/assign/retain/copy conflict.
18429d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
184328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
184428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                     ObjCDeclSpec::DQ_PR_assign |
1845f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_unsafe_unretained |
184628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                     ObjCDeclSpec::DQ_PR_copy |
1847f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_retain |
1848f85e193739c953358c865005855253af4f68a497John McCall                     ObjCDeclSpec::DQ_PR_strong))) {
184928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
185028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "readwrite" :
185128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                         (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
185228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "assign" :
1853f85e193739c953358c865005855253af4f68a497John McCall                         (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1854f85e193739c953358c865005855253af4f68a497John McCall                          "unsafe_unretained" :
185528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                         (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
185628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                          "copy" : "retain";
18579d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
185828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
185928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                 diag::err_objc_property_attr_mutually_exclusive :
186028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek                 diag::warn_objc_property_attr_mutually_exclusive)
186128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      << "readonly" << which;
186228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
18639d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
186428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Check for copy or retain on non-object types.
1865f85e193739c953358c865005855253af4f68a497John McCall  if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1866f85e193739c953358c865005855253af4f68a497John McCall                    ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1867f85e193739c953358c865005855253af4f68a497John McCall      !PropertyTy->isObjCRetainableType() &&
1868842f07b065ac481fce9d82d55cb62e36bac6c921Fariborz Jahanian      !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
186928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, diag::err_objc_property_requires_object)
1870f85e193739c953358c865005855253af4f68a497John McCall      << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1871f85e193739c953358c865005855253af4f68a497John McCall          Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1872f85e193739c953358c865005855253af4f68a497John McCall    Attributes &= ~(ObjCDeclSpec::DQ_PR_weak   | ObjCDeclSpec::DQ_PR_copy |
1873f85e193739c953358c865005855253af4f68a497John McCall                    ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
1874977ea7864a50eee39f6bef8a5a79154ac9ba4b3eJohn McCall    PropertyDecl->setInvalidDecl();
187528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
18769d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
187728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Check for more than one of { assign, copy, retain }.
187828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
187928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
188028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
188128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "assign" << "copy";
188228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
188328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
188428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
188528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
188628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "assign" << "retain";
188728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
188828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
1889f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1890f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1891f85e193739c953358c865005855253af4f68a497John McCall        << "assign" << "strong";
1892f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1893f85e193739c953358c865005855253af4f68a497John McCall    }
18944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount  &&
1895f85e193739c953358c865005855253af4f68a497John McCall        (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1896f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1897f85e193739c953358c865005855253af4f68a497John McCall        << "assign" << "weak";
1898f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1899f85e193739c953358c865005855253af4f68a497John McCall    }
1900f85e193739c953358c865005855253af4f68a497John McCall  } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1901f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1902f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1903f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "copy";
1904f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1905f85e193739c953358c865005855253af4f68a497John McCall    }
1906f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1907f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1908f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "retain";
1909f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1910f85e193739c953358c865005855253af4f68a497John McCall    }
1911f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1912f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1913f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "strong";
1914f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1915f85e193739c953358c865005855253af4f68a497John McCall    }
19164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount  &&
1917f85e193739c953358c865005855253af4f68a497John McCall        (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1918f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1919f85e193739c953358c865005855253af4f68a497John McCall        << "unsafe_unretained" << "weak";
1920f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1921f85e193739c953358c865005855253af4f68a497John McCall    }
192228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
192328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
192428685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
192528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek        << "copy" << "retain";
192628685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
192728685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    }
1928f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1929f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1930f85e193739c953358c865005855253af4f68a497John McCall        << "copy" << "strong";
1931f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1932f85e193739c953358c865005855253af4f68a497John McCall    }
1933f85e193739c953358c865005855253af4f68a497John McCall    if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1934f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1935f85e193739c953358c865005855253af4f68a497John McCall        << "copy" << "weak";
1936f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1937f85e193739c953358c865005855253af4f68a497John McCall    }
1938f85e193739c953358c865005855253af4f68a497John McCall  }
1939f85e193739c953358c865005855253af4f68a497John McCall  else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1940f85e193739c953358c865005855253af4f68a497John McCall           (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1941f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1942f85e193739c953358c865005855253af4f68a497John McCall        << "retain" << "weak";
1943528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1944f85e193739c953358c865005855253af4f68a497John McCall  }
1945f85e193739c953358c865005855253af4f68a497John McCall  else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1946f85e193739c953358c865005855253af4f68a497John McCall           (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1947f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1948f85e193739c953358c865005855253af4f68a497John McCall        << "strong" << "weak";
1949f85e193739c953358c865005855253af4f68a497John McCall      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
195028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
19519d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
19529d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian  if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
19539d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
19549d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
19559d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian        << "atomic" << "nonatomic";
19569d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian      Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
19579d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian  }
19589d1bbeab2475fe45a3d4cb8de6810bc3275f1dd7Fariborz Jahanian
195928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // Warn if user supplied no assignment attribute, property is
196028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  // readwrite, and this is an object type.
196128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1962f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_unsafe_unretained |
1963f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1964f85e193739c953358c865005855253af4f68a497John McCall                      ObjCDeclSpec::DQ_PR_weak)) &&
196528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      PropertyTy->isObjCObjectPointerType()) {
19664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount)
1967bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian        // With arc,  @property definitions should default to (strong) when
1968f21a92d35953adb038ffbe37cd1a4083e0df0ec0Fariborz Jahanian        // not specified; including when property is 'readonly'.
1969bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian        PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1970f21a92d35953adb038ffbe37cd1a4083e0df0ec0Fariborz Jahanian      else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
19719f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        bool isAnyClassTy =
19729f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian          (PropertyTy->isObjCClassType() ||
19739f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian           PropertyTy->isObjCQualifiedClassType());
19749f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
19759f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        // issue any warning.
19764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
19779f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian          ;
19789f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        else {
1979bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian          // Skip this warning in gc-only mode.
19804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (getLangOpts().getGC() != LangOptions::GCOnly)
1981bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian            Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
19820a68dc7f04be1542ce249ff4adb169453698ad91Argyrios Kyrtzidis
1983bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian          // If non-gc code warn that this is likely inappropriate.
19844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (getLangOpts().getGC() == LangOptions::NonGC)
1985bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian            Diag(Loc, diag::warn_objc_property_default_assign_on_object);
19869f37cd1810646ffc9002c7a6477fe158ee15ede4Fariborz Jahanian        }
1987bc03aea4bec84d7abecabd26b8583adb687c4089Fariborz Jahanian      }
19889d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
198928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // FIXME: Implement warning dependent on NSCopying being
199028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // implemented. See also:
199128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
199228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    // (please trim this list while you are at it).
199328685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  }
19949d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek
199528685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek  if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
19962b77cb803c76d01d2d10a58cec9334e2090cd7c3Fariborz Jahanian      &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
19974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      && getLangOpts().getGC() == LangOptions::GCOnly
199828685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek      && PropertyTy->isBlockPointerType())
199928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek    Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
20004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  else if (getLangOpts().ObjCAutoRefCount &&
2001528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2002528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2003528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2004528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian           PropertyTy->isBlockPointerType())
2005528a499eb84d61667f65b16a13780c135b822f6bFariborz Jahanian      Diag(Loc, diag::warn_objc_property_retain_of_block);
200648a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian
200748a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
200848a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian      (Attributes & ObjCDeclSpec::DQ_PR_setter))
200948a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian    Diag(Loc, diag::warn_objc_readonly_property_has_setter);
201048a98c7a8467a9570d2fc7f2aab7f5273a3e218eFariborz Jahanian
20119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek}
2012