]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR objc/7993 (private variables cannot be shadowed in subclasses)
authorNicola Pero <n.pero@mi.flashnet.it>
Wed, 14 Jan 2004 07:52:39 +0000 (08:52 +0100)
committerGabriel Dos Reis <gdr@gcc.gnu.org>
Wed, 14 Jan 2004 07:52:39 +0000 (07:52 +0000)
PR objc/7993
        * objc-act.c (is_private): Do not emit the 'instance variable %s
        is declared private' error.
        (is_public): Emit the error after calling is_private.
        (lookup_objc_ivar): If the instance variable is private, return 0
        - the instance variable is invisible here.

From-SVN: r75850

gcc/ChangeLog
gcc/objc/objc-act.c
gcc/testsuite/objc.dg/private-1.m [new file with mode: 0644]
gcc/testsuite/objc.dg/private-2.m [new file with mode: 0644]

index 5c8efb3f7ea01bf345a26e51d2389f8ac80b8fb0..d6d3a762191471f4866e042d6fe3f63b47a11cba 100644 (file)
@@ -1,3 +1,12 @@
+2003-01-14  Nicola Pero  <n.pero@mi.flashnet.it>
+
+       PR objc/7993
+        * objc-act.c (is_private): Do not emit the 'instance variable %s
+        is declared private' error.
+        (is_public): Emit the error after calling is_private.
+        (lookup_objc_ivar): If the instance variable is private, return 0
+        - the instance variable is invisible here.
+       
 2004-01-12  Marc Espie <espie@openbsd.org>
 
        * system.h: handle YYBYACC like YYBISON.
index 018ba04f9998efd753d93b29e8cf07abbf081b23..3ad0cc60ee150657e958217559c03376d7b04591 100644 (file)
@@ -5848,11 +5848,7 @@ is_private (decl)
 {
   if (TREE_PRIVATE (decl)
       && ! is_ivar (CLASS_IVARS (implementation_template), DECL_NAME (decl)))
-    {
-      error ("instance variable `%s' is declared private",
-            IDENTIFIER_POINTER (DECL_NAME (decl)));
-      return 1;
-    }
+    return 1;
   else
     return 0;
 }
@@ -5894,7 +5890,14 @@ is_public (expr, identifier)
                           == CATEGORY_IMPLEMENTATION_TYPE))
                      && (CLASS_NAME (objc_implementation_context)
                          == TYPE_NAME (basetype))))
-               return ! is_private (decl);
+                {
+                  int private = is_private (decl);
+                  if (private)
+                    error ("instance variable '%s' si declared private",
+                           IDENTIFIER_POINTER (DECL_NAME (decl)));
+
+                  return !private;
+                }
 
              error ("instance variable `%s' is declared %s",
                     IDENTIFIER_POINTER (identifier),
@@ -8524,7 +8527,7 @@ lookup_objc_ivar (id)
   else if (objc_method_context && (decl = is_ivar (objc_ivar_chain, id)))
     {
       if (is_private (decl))
-       return error_mark_node;
+       return 0;
       else
         return build_ivar_reference (id);
     }
diff --git a/gcc/testsuite/objc.dg/private-1.m b/gcc/testsuite/objc.dg/private-1.m
new file mode 100644 (file)
index 0000000..64b3618
--- /dev/null
@@ -0,0 +1,59 @@
+/* Test errors for accessing @private and @protected variables.  */
+/* Author: Nicola Pero <nicola@brainstorm.co.uk>.  */
+/* { dg-do compile } */
+#include <objc/objc.h>
+
+@interface MySuperClass
+{
+@private
+  int private;
+
+@protected
+  int protected;
+
+@public
+  int public;
+}
+- (void) test;
+@end
+
+@implementation MySuperClass
+- (void) test
+{
+  private = 12;   /* Ok  */
+  protected = 12; /* Ok  */
+  public = 12;    /* Ok  */
+}
+@end
+
+
+@interface MyClass : MySuperClass 
+@end
+
+@implementation MyClass
+- (void) test
+{
+  /* Private variables simply don't exist in the subclass.  */
+  private = 12;/* { dg-error "undeclared" } */
+  /* { dg-error "function it appears in" "" { target *-*-* } { 37 } } */
+
+  protected = 12; /* Ok  */
+  public = 12;    /* Ok  */
+}
+@end
+
+int main (void)
+{
+  MyClass *m = nil;
+  
+  if (m != nil)
+    {
+      int access;
+
+      access = m->private;   /* { dg-error "is declared private" }  */
+      access = m->protected; /* { dg-error "is declared protected" }  */
+      access = m->public;    /* Ok  */
+    }
+
+  return 0;
+}
diff --git a/gcc/testsuite/objc.dg/private-2.m b/gcc/testsuite/objc.dg/private-2.m
new file mode 100644 (file)
index 0000000..eff376a
--- /dev/null
@@ -0,0 +1,54 @@
+/* Test warnings for shadowing instance variables.  */
+/* Author: Nicola Pero <nicola@brainstorm.co.uk>.  */
+/* { dg-do compile } */
+#include <objc/objc.h>
+
+@interface MySuperClass
+{
+@private
+  int private;
+
+@protected
+  int protected;
+
+@public
+  int public;
+}
+- (void) test;
+@end
+
+@implementation MySuperClass
+- (void) test
+{
+  /* FIXME: I wonder if the warnings shouldn't be better generated
+     when the variable is declared, rather than used!  */
+  int private = 12;
+  int protected = 12;
+  int public = 12;
+  int a;
+  
+  a = private;    /* { dg-warning "hides instance variable" } */
+  a = protected;  /* { dg-warning "hides instance variable" } */
+  a = public;     /* { dg-warning "hides instance variable" } */
+}
+@end
+
+
+@interface MyClass : MySuperClass 
+@end
+
+@implementation MyClass
+- (void) test
+{
+  int private = 12;
+  int protected = 12;
+  int public = 12;
+  int a;
+
+  /* The private variable can be shadowed without warnings, because
+   * it's invisible, and not accessible, to the subclass!  */
+  a = private;   /* Ok  */
+  a = protected; /* { dg-warning "hides instance variable" } */
+  a = public;    /* { dg-warning "hides instance variable" } */
+}
+@end