]> granicus.if.org Git - clang/commitdiff
Patch to supprt case of readonly property being
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 12 Jan 2009 19:55:42 +0000 (19:55 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 12 Jan 2009 19:55:42 +0000 (19:55 +0000)
assigned to when it has user declared setter method
defined in the class implementation (but no declaration in
the class itself).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62098 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclObjC.h
lib/AST/DeclObjC.cpp
lib/AST/Expr.cpp
lib/Sema/Sema.h
lib/Sema/SemaDeclObjC.cpp
lib/Sema/SemaExpr.cpp
test/SemaObjC/property-user-setter.m

index b83f51ad53f8aaec9b0d89ef71603715eee688f7..9139050660771b756043aa10162b015feef04a70 100644 (file)
@@ -405,7 +405,6 @@ public:
   
   ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
   ObjCIvarDecl *FindIvarDeclaration(IdentifierInfo *IvarId) const;
-  bool isPropertyReadonly(ObjCPropertyDecl *PropertyDecl) const;
 
   typedef ObjCList<ObjCProtocolDecl>::iterator protocol_iterator;
   protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
index c4caed1e00ffd9bf4a18c46558a95cf100854af6..9fc0f763bbd90586e58fccac0ad688a0feea04d9 100644 (file)
@@ -251,34 +251,6 @@ void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
   }
 }
 
-/// isPropertyReadonly - Return true if property is readonly, by searching
-/// for the property in the class and in its categories.
-///
-bool ObjCInterfaceDecl::isPropertyReadonly(ObjCPropertyDecl *PDecl) const
-{
-  // Even if property is ready only, if interface has a user defined setter, 
-  // it is not considered read only. 
-  if (!PDecl->isReadOnly() || getInstanceMethod(PDecl->getSetterName()))
-    return false;
-
-  // Main class has the property as 'readonly'. Must search
-  // through the category list to see if the property's 
-  // attribute has been over-ridden to 'readwrite'.
-  for (ObjCCategoryDecl *Category = getCategoryList();
-       Category; Category = Category->getNextClassCategory()) {
-    // Even if property is ready only, if a category has a user defined setter, 
-    // it is not considered read only. 
-    if (Category->getInstanceMethod(PDecl->getSetterName()))
-      return false;
-    ObjCPropertyDecl *P = 
-      Category->FindPropertyDeclaration(PDecl->getIdentifier());
-    if (P && !P->isReadOnly())
-      return false;
-  }
-
-  return true;
-}
-
 /// FindCategoryDeclaration - Finds category declaration in the list of
 /// categories for this class and returns it. Name of the category is passed
 /// in 'CategoryId'. If category not found, return 0;
index 26260132e7ce447f83b557d5fa7ec5c18c37314e..89cad4197914e6e051bc72111b0b76d4505580ef 100644 (file)
@@ -604,19 +604,7 @@ Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
     if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
       return MLV_NotBlockQualified;
   }
-  // Assigning to a readonly property?
-  if (getStmtClass() == ObjCPropertyRefExprClass) {
-    const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(this);
-    if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) {
-      QualType BaseType = PropExpr->getBase()->getType();
-      if (const PointerType *PTy = BaseType->getAsPointerType())
-        if (const ObjCInterfaceType *IFTy = 
-            PTy->getPointeeType()->getAsObjCInterfaceType())
-          if (ObjCInterfaceDecl *IFace = IFTy->getDecl())
-            if (IFace->isPropertyReadonly(PDecl))
-              return MLV_ReadonlyProperty;
-    }
-  }
+  
   // Assigning to an 'implicit' property?
   else if (getStmtClass() == ObjCKVCRefExprClass) {
     const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
index 4db6f646b6a349505e87b8452e96877cd7169518..f098372d97f4cc666dab1f72d856f6bbae54c20f 100644 (file)
@@ -538,6 +538,9 @@ public:
                                    ObjCMethodDecl *IntfMethod);
 
   NamespaceDecl *GetStdNamespace();
+  
+  bool isPropertyReadonly(ObjCPropertyDecl *PropertyDecl,
+                          ObjCInterfaceDecl *IDecl) const;
                            
   /// CheckProtocolMethodDefs - This routine checks unimplemented
   /// methods declared in protocol, and those referenced by it.
index c4b085dc1eb679e83ce9922df61aa053a7327c99..afcf45622b917c916776adfc229ea45273557ebd 100644 (file)
@@ -699,6 +699,51 @@ void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
   }
 }
 
+/// isPropertyReadonly - Return true if property is readonly, by searching
+/// for the property in the class and in its categories and implementations
+///
+bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
+                              ObjCInterfaceDecl *IDecl) const {
+  // by far the most common case.
+  if (!PDecl->isReadOnly())
+    return false;
+  // Even if property is ready only, if interface has a user defined setter, 
+  // it is not considered read only.
+  if (IDecl->getInstanceMethod(PDecl->getSetterName()))
+    return false;
+  
+  // Main class has the property as 'readonly'. Must search
+  // through the category list to see if the property's 
+  // attribute has been over-ridden to 'readwrite'.
+  for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
+       Category; Category = Category->getNextClassCategory()) {
+    // Even if property is ready only, if a category has a user defined setter, 
+    // it is not considered read only. 
+    if (Category->getInstanceMethod(PDecl->getSetterName()))
+      return false;
+    ObjCPropertyDecl *P = 
+    Category->FindPropertyDeclaration(PDecl->getIdentifier());
+    if (P && !P->isReadOnly())
+      return false;
+  }
+  
+  // Also, check for definition of a setter method in the implementation if
+  // all else failed.
+  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
+    if (ObjCImplementationDecl *IMD = 
+        dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
+      if (IMD->getInstanceMethod(PDecl->getSetterName()))
+        return false;
+    }
+    else if (ObjCCategoryImplDecl *CIMD = 
+             dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
+      if (CIMD->getInstanceMethod(PDecl->getSetterName()))
+        return false;
+    }
+  }
+  return true;
+}
+
 /// FIXME: Type hierarchies in Objective-C can be deep. We could most
 /// likely improve the efficiency of selector lookups and type
 /// checking by associating with each protocol / interface / category
index bf04042ef8a991d56f204dbf7799e74ad96ad7d7..d3d3d3556c40150af32319feec3ffe5db48a7127 100644 (file)
@@ -2920,10 +2920,33 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
   return InvalidOperands(Loc, lex, rex);
 }
 
+/// IsReadonlyProperty - Verify that otherwise a valid l-value expression
+/// is a read-only property; return true if so. A readonly property expression
+/// depends on various declarations and thus must be treated specially.
+///
+static bool IsReadonlyProperty(Expr *E, Sema &S) 
+{
+  if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) {
+    const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E);
+    if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) {
+      QualType BaseType = PropExpr->getBase()->getType();
+      if (const PointerType *PTy = BaseType->getAsPointerType())
+        if (const ObjCInterfaceType *IFTy = 
+            PTy->getPointeeType()->getAsObjCInterfaceType())
+          if (ObjCInterfaceDecl *IFace = IFTy->getDecl())
+            if (S.isPropertyReadonly(PDecl, IFace))
+              return true;
+    }
+  }
+  return false;
+}
+
 /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue.  If not,
 /// emit an error and return true.  If so, return false.
 static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) {
-  Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); 
+  Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context);
+  if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S))
+    IsLV = Expr::MLV_ReadonlyProperty;
   if (IsLV == Expr::MLV_Valid)
     return false;
   
index cb2091e8cedb9452ff8e8f914944435c9c292bda..d4da1dfed9f4607527f022926192cff01cafb0a4 100644 (file)
   self.z = 2; // expected-error {{assigning to property with 'readonly' attribute not allowed}}
 }
 @end
+
+// Test when property is 'readonly' but it has a setter in
+// its implementation only.
+@interface I1  {
+}
+@property(readonly) int identifier;
+@end
+
+
+@implementation I1
+@dynamic identifier;
+- (void)setIdentifier:(int)ident {}
+
+- (id)initWithIdentifier:(int)Arg {
+    self.identifier = 0;
+}
+
+@end
+
+
+// Also in a category implementation
+@interface I1(CAT)  
+@property(readonly) int rprop;
+@end
+
+
+@implementation I1(CAT)
+@dynamic rprop;
+- (void)setRprop:(int)ident {}
+
+- (id)initWithIdentifier:(int)Arg {
+    self.rprop = 0;
+}
+
+@end
+