Patch: gcj -vs- protected

Tom Tromey tromey@redhat.com
Tue Jan 16 20:10:00 GMT 2001


This patch fixes up the `protected' access specifier in gcj.
It fixes PR java/1261.

I verified this against the bug report, some other tests I wrote, and
by rebuilding libgcj from scratch.

Alex, I didn't write ChangeLog entries for the parts of the patch that
you wrote (the changes to resolve_class and build_java_array_type),
since I don't really understand them :-(

Ok to commit?  I mean, once the ChangeLog issue is resolved?

I think this deserves to go into gcc 3.0 because it is a user-visible
front end change.

2001-01-15  Tom Tromey  <tromey@redhat.com>
            Alexandre Petit-Bianco  <apbianco@cygnus.com>

	Fix for PR java/1261.
	* typeck.c (build_java_array_type): Add public `clone' method to
	arrays.
	* parse.y (resolve_qualified_expression_name): Use current_class
	when checking for inaccessibility.
	(patch_method_invocation): Fixed error message when accessibility
	denied.  Added `from_super' argument.

Tom

Index: parse.y
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/parse.y,v
retrieving revision 1.243
diff -u -r1.243 parse.y
--- parse.y	2001/01/15 08:01:22	1.243
+++ parse.y	2001/01/17 04:01:47
@@ -126,7 +126,7 @@
 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
 static int check_class_interface_creation PARAMS ((int, int, tree, 
 						  tree, tree, tree));
-static tree patch_method_invocation PARAMS ((tree, tree, tree, 
+static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
 					    int *, tree *));
 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
 static tree resolve_and_layout PARAMS ((tree, tree));
@@ -5494,14 +5522,9 @@
 	  CLASS_LOADED_P (resolved_type) = 1;
 	  name--;
 	}
-      /* Build a fake decl for this, since this is what is expected to
-         be returned.  */
-      resolved_type_decl =
-	build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
-      /* Figure how those two things are important for error report. FIXME */
-      DECL_SOURCE_LINE (resolved_type_decl) = 0;
-      DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
-      TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
+      /* A TYPE_NAME that is a TYPE_DECL was set in
+         build_java_array_type, return it. */
+      resolved_type_decl = TYPE_NAME (resolved_type);
     }
   TREE_TYPE (class_type) = resolved_type;
   return resolved_type_decl;
@@ -9194,7 +9217,8 @@
 	    CALL_USING_SUPER (qual_wfl) = 1;
 	  location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
 		      EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
-	  *where_found = patch_method_invocation (qual_wfl, decl, type, 
+	  *where_found = patch_method_invocation (qual_wfl, decl, type,
+						  from_super,
 						  &is_static, &ret_decl);
 	  if (*where_found == error_mark_node)
 	    {
@@ -9566,7 +9590,7 @@
 		CLASS_LOADED_P (field_decl_type) = 1;
 	      
 	      /* Check on accessibility here */
-	      if (not_accessible_p (type, field_decl, from_super))
+	      if (not_accessible_p (current_class, field_decl, from_super))
 		{
 		  parse_error_context 
 		    (qual_wfl,
@@ -9822,8 +9846,10 @@
    used. IS_STATIC is set to 1 if the invoked function is static. */
 
 static tree
-patch_method_invocation (patch, primary, where, is_static, ret_decl)
+patch_method_invocation (patch, primary, where, from_super,
+			 is_static, ret_decl)
      tree patch, primary, where;
+     int from_super;
      int *is_static;
      tree *ret_decl;
 {
@@ -10057,19 +10083,20 @@
 
   /* Check accessibility, position the is_static flag, build and
      return the call */
-  if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
+  if (not_accessible_p (DECL_CONTEXT (current_function_decl),
+			list, from_super))
     {
-      char *fct_name = xstrdup (lang_printable_name (list, 0));
-      int ctor_p = DECL_CONSTRUCTOR_P (list);
-      parse_error_context 
-	(wfl, "Can't access %s %s `%s%s.%s' from `%s'",
-	 java_accstring_lookup (get_access_flags_from_decl (list)),
-	 (ctor_p ? "constructor" : "method"),
-	 (ctor_p ? 
-	  "" : lang_printable_name_wls (TREE_TYPE (TREE_TYPE (list)), 0)), 
-	 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))), 
-	 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
-      free (fct_name);
+      char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
+      char *access = java_accstring_lookup (get_access_flags_from_decl (list));
+      char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
+      char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
+      char *what = (char *) (DECL_CONSTRUCTOR_P (list)
+			     ? "constructor" : "method");
+      /* FIXME: WFL yields the wrong message here but I don't know
+	 what else to use.  */
+      parse_error_context (wfl,
+			   "Can't access %s %s `%s.%s' from `%s'",
+			   access, what, klass, fct_name, refklass);
       PATCH_METHOD_RETURN_ERROR ();
     }
   check_deprecation (wfl, list);
@@ -11558,9 +11585,11 @@
 	{
 	  tree decl, wfl = TREE_OPERAND (node, 0);
 	  int in_this = CALL_THIS_CONSTRUCTOR_P (node);
+	  int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
+			    super_identifier_node);
 
-	  node = patch_method_invocation (node, NULL_TREE, 
-					  NULL_TREE, 0, &decl);
+	  node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
+					  from_super, 0, &decl);
 	  if (node == error_mark_node)
 	    return error_mark_node;
 
@@ -11611,7 +11640,7 @@
              class. TESTME, FIXME */
 	  tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); 
 
-	  /* Hand stablize the lhs on both places */
+	  /* Hand stabilize the lhs on both places */
 	  TREE_OPERAND (node, 0) = lvalue;
 	  TREE_OPERAND (TREE_OPERAND (node, 1), 0) = 
 	    (flag_emit_class_files ? lvalue : save_expr (lvalue));
Index: typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/typeck.c,v
retrieving revision 1.33
diff -u -r1.33 typeck.c
--- typeck.c	2000/10/13 06:26:46	1.33
+++ typeck.c	2001/01/17 04:01:48
@@ -1,5 +1,5 @@
 /* Handle types for the GNU compiler for the Java(TM) language.
-   Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -398,7 +398,9 @@
   el_name = TYPE_NAME (el_name);
   if (TREE_CODE (el_name) == TYPE_DECL)
     el_name = DECL_NAME (el_name);
-  TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
+  TYPE_NAME (t) = build_decl (TYPE_DECL,
+			      identifier_subst (el_name, "", '.', '.', "[]"),
+			      t);
 
   set_java_signature (t, sig);
   set_super_info (0, t, object_type_node, 0);
@@ -412,6 +414,12 @@
   DECL_CONTEXT (fld) = t;
   FIELD_PUBLIC (fld) = 1;
   FIELD_FINAL (fld) = 1;
+  TREE_READONLY (fld) = 1;
+
+  /* Add clone method.  This is different from Object.clone because it
+     is public.  */
+  add_method (t, ACC_PUBLIC | ACC_FINAL, get_identifier ("clone"),
+	      get_identifier ("()Ljava/lang/Object;"));
 
   if (length >= 0)
     {


More information about the Gcc-patches mailing list