This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: PR java/9685, 13098, 15073 - Catch Illegal Package-PrivateAccesses
- From: Ranjit Mathew <rmathew at gmail dot com>
- To: java-patches at gcc dot gnu dot org
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Sun, 25 Apr 2004 01:12:39 +0530
- Subject: Patch: PR java/9685, 13098, 15073 - Catch Illegal Package-PrivateAccesses
Hi,
This patch is a revision of my proposed fix for
PR java/9685 to accommodate my proposed fix for
PR java/15073. Doing this allows me to remove the
kludge in my previous patch to not_accessible_p()
in parse.y to take care of TYPE_DECLs being passed
to it, despite it being present only to handle
fields/methods - classes and interfaces are supposed
to be handled by check_pkg_class_access().
The previous patch and subsequent discussion about
it can be found in:
Bootstrapped with no regressions (libjava) on
i686-pc-linux-gnu. The Jacks testsuite shows 5
additional successes and 2 new failures because of
this patch - see the above thread for discussion
on this.
PR java/13098 is essentially a duplicate of 9685.
Ok for mainline?
Ranjit.
Index: ChangeLog
from Ranjit Mathew <rmathew@hotmail.com>
Fixes PR java/9685, PR java/13098, PR java/15073
* parse.y (accessibility_string): New method.
(not_accessible_field_error): Use accessibility_string()
instead of java_accstring_lookup().
(resolve_qualified_expression_name): Check with
check_pkg_class_access() before allowing access using
qualified names.
Fix comment typo.
Use check_pkg_class_access() instead of not_accessible_p()
for unqualified types.
(not_accessible_p): Use DECL_CONTEXT (member) instead of
REFERENCE for package-private access checking.
(patch_method_invocation): Use accessibility_string() instead
of java_accstring_lookup().
Index: parse.y
===================================================================
--- parse.y 2004-04-24 20:43:39.000000000 +0530
+++ parse.y 2004-04-24 22:32:10.000000000 +0530
@@ -76,6 +76,7 @@ definitions and other extensions. */
/* Local function prototypes */
static char *java_accstring_lookup (int);
+static const char *accessibility_string (int);
static void classitf_redefinition_error (const char *,tree, tree, tree);
static void variable_redefinition_error (tree, tree, tree, int);
static tree create_class (int, tree, tree, tree);
@@ -3182,7 +3183,7 @@ not_accessible_field_error (tree wfl, tr
{
parse_error_context
(wfl, "Can't access %s field `%s.%s' from `%s'",
- java_accstring_lookup (get_access_flags_from_decl (decl)),
+ accessibility_string (get_access_flags_from_decl (decl)),
GET_TYPE_NAME (DECL_CONTEXT (decl)),
IDENTIFIER_POINTER (DECL_NAME (decl)),
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
@@ -3228,6 +3229,22 @@ java_accstring_lookup (int flags)
#undef COPY_RETURN
}
+/* Returns a string denoting the accessibility of a class or a member as
+ indicated by FLAGS. We need a separate function from
+ java_accstring_lookup, as the latter can return spurious "static", etc.
+ if package-private access is defined (in which case none of the
+ relevant access control bits in FLAGS is set). */
+
+static const char *
+accessibility_string (int flags)
+{
+ if (flags & ACC_PRIVATE) return "private";
+ if (flags & ACC_PROTECTED) return "protected";
+ if (flags & ACC_PUBLIC) return "public";
+
+ return "package-private";
+}
+
/* Issuing error messages upon redefinition of classes, interfaces or
variables. */
@@ -9819,6 +9836,8 @@ resolve_qualified_expression_name (tree
tree list;
*where_found = decl;
+ check_pkg_class_access (DECL_NAME (decl), qual_wfl, true);
+
/* We want to be absolutely sure that the class is laid
out. We're going to search something inside it. */
*type_found = type = TREE_TYPE (decl);
@@ -9859,8 +9878,8 @@ resolve_qualified_expression_name (tree
decl = QUAL_RESOLUTION (q);
/* Sneak preview. If next we see a `new', we're facing a
- qualification with resulted in a type being selected
- instead of a field. Report the error */
+ qualification which resulted in a type being selected
+ instead of a field. Report the error. */
if(TREE_CHAIN (q)
&& TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
{
@@ -9869,15 +9888,8 @@ resolve_qualified_expression_name (tree
return 1;
}
- if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
- {
- parse_error_context
- (qual_wfl, "Can't access %s class '%s' from '%s'",
- java_accstring_lookup (get_access_flags_from_decl (decl)),
- IDENTIFIER_POINTER (DECL_NAME (decl)),
- IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
- return 1;
- }
+ check_pkg_class_access (DECL_NAME (decl), qual_wfl, true);
+
check_deprecation (qual_wfl, decl);
type = TREE_TYPE (decl);
@@ -10140,14 +10152,9 @@ not_accessible_p (tree reference, tree m
return 1;
}
- /* Default access are permitted only when occurring within the
- package in which the type (REFERENCE) is declared. In other words,
- REFERENCE is defined in the current package */
- if (ctxp->package)
- return !class_in_current_package (reference);
-
- /* Otherwise, access is granted */
- return 0;
+ /* Default access is permitted only when occurring from within the
+ package in which the context (MEMBER) is declared. */
+ return !class_in_current_package (DECL_CONTEXT (member));
}
/* Test deprecated decl access. */
@@ -10540,7 +10547,7 @@ patch_method_invocation (tree patch, tre
{
const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
const char *const access =
- java_accstring_lookup (get_access_flags_from_decl (list));
+ accessibility_string (get_access_flags_from_decl (list));
const char *const klass =
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
const char *const refklass =