]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR objc/7993 (private variables cannot be shadowed in subclasses)
authorNicola Pero <nicola@brainstorm.co.uk>
Tue, 1 Jun 2004 07:40:02 +0000 (07:40 +0000)
committerZack Weinberg <zack@gcc.gnu.org>
Tue, 1 Jun 2004 07:40:02 +0000 (07:40 +0000)
2004-06-01  Nicola Pero  <nicola@brainstorm.co.uk>

  Fix 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.
testsuite:
* objc.dg/private-1.m, objc-dg/private-2.m: New testcases.

From-SVN: r82532

gcc/ChangeLog
gcc/objc/objc-act.c
gcc/testsuite/ChangeLog
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 8d3b14975773343dbb5aec6b670d10e232432876..9e3fd3b527145634c712bc1a08769811635c25b6 100644 (file)
@@ -1,3 +1,12 @@
+2004-06-01  Nicola Pero  <nicola@brainstorm.co.uk>
+
+       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-06-01  Eric Botcazou  <ebotcazou@libertysurf.fr>
 
        * doc/invoke.texi (-static-libgcc): Explicitly mention
        and the remaining even if flag_unsafe_math_optimizations
        is off but we are under -fno-trapping-math.
        (fold_relational_const): Integer modes do not honor NaNs.
-       
+
 2004-05-28  Paul Brook  <paul@codesourcery.com>
 
        * config/arm/arm.c (arm_output_epilogue): Remove redundant code.
        calculate_local_reg_bb_info, set_up_bb_rts_numbers, rpost_cmp,
        modify_bb_reg_pav, calculate_reg_pav,
        make_accurate_live_analysis): New functions.
-       
+
 2004-05-25  Devang Patel  <dpatel@apple.com>
 
        * alias.c (init_alias_analysis): Use ggc_calloc instead of
        xrealloc.
        (end_alias_analysis): Use ggc_free instead fo free.
-       
+
 2004-05-25  Andrew Pinski  <pinskia@physics.uc.edu>
 
        PR target/15546
        (nonzero_bits, cached_nonzero_bits, nonzero_bits1,
        num_sign_bit_copies, cached_num_sign_bit_copies,
        num_sign_bit_copies1): New, from combine.c.
-       * rtlhooks.c: New file. 
+       * rtlhooks.c: New file.
 
 2004-05-25  Svein E. Seldal  <Svein.Seldal@solidas.com>
 
        to support 32-bit -mint8 mode.
 
        * doc/invoke.texi (-mint8): Added documentation for the -mint8
-       option in the AVR architecture. 
+       option in the AVR architecture.
 
 2004-05-24  Mike Stump  <mrs@apple.com>
 
 
        PR tree-optimization/14197
        * builtins.c: Include "tree-gimple.h"
-       (readonly_data_expr): Use get_base_address.  Make sure to call 
+       (readonly_data_expr): Use get_base_address.  Make sure to call
        decl_readonly_section only on trees it can handle.
        * tree-gimple.c (get_base_address): Accept STRING_CST and
        CONSTRUCTOR expressions.
index 385360a28ebcc987948192a42629d3c12ebb07e5..07074330adfe60df4283628bd19a9ff0a4cdadd6 100644 (file)
@@ -6467,15 +6467,9 @@ is_ivar (tree decl_chain, tree ident)
 int
 is_private (tree 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;
-    }
-  else
-    return 0;
+  return (TREE_PRIVATE (decl)
+         && ! is_ivar (CLASS_IVARS (implementation_template),
+                       DECL_NAME (decl)));
 }
 
 /* We have an instance variable reference;, check to see if it is public.  */
@@ -6513,7 +6507,14 @@ is_public (tree expr, tree identifier)
                           == CATEGORY_IMPLEMENTATION_TYPE))
                      && (CLASS_NAME (objc_implementation_context)
                          == OBJC_TYPE_NAME (basetype))))
-               return ! is_private (decl);
+               {
+                 int private = is_private (decl);
+
+                 if (private)
+                   error ("instance variable `%s' is declared private",
+                          IDENTIFIER_POINTER (DECL_NAME (decl)));
+                 return !private;
+               }
 
              /* The 2.95.2 compiler sometimes allowed C functions to access
                 non-@public ivars.  We will let this slide for now...  */
@@ -9066,7 +9067,7 @@ lookup_objc_ivar (tree 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);
     }
index 67b637b3ac1063db23b537f00398eed2b605b9cc..0c34a57a740afe2c2f496d9ffdd9886a88cbdb53 100644 (file)
@@ -1,3 +1,8 @@
+2004-06-01  Nicola Pero  <nicola@brainstorm.co.uk>
+
+       PR objc/7993
+       * objc.dg/private-1.m, objc-dg/private-2.m: New testcases.
+
 2004-05-31  Mark Mitchell  <mark@codesourcery.com>
 
        PR c++/15701
        * gfortran.fortran-torture/execute/power.f90: Test constant integers.
 
 2004-05-18  Andrew Pinski  <pinskia@physics.uc.edu>
-            Jeff Law  <law@redhat.com>
+           Jeff Law  <law@redhat.com>
 
        * gcc.dg/tree-ssa/20040514-2.c: Update expected output.
        * gcc.dg/tree-ssa/20040518-2.c: New test.
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..f4d8a52
--- /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 @private" }  */
+      access = m->protected; /* { dg-error "is @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