This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

patch for inner class method search


(This is a follow up to the message "partial patch for inner class
method search" sent to the Java list April 21.)

I've checked this (which is similar but not the same as my April 21
patch) into both the trunk and the branch.  Alex, it would still be
good if you could take a look at it.

The testsuite has improved slightly:

# of expected passes            1674
# of unexpected successes       12
# of expected failures          14
# of untested testcases         16

2001-04-26  Per Bothner  <per@bothner.com>

	Fix method search wrt scope of inner classes to match JLS2.
	* typeck.c (build_null_signature):  New static function.
	(has_method):  New function.  Uses build_null_signature and lookup_do.
	* java-tree.h (has_method):  New declaration.
	* parse.y (find_applicable_accessible_methods_list):  Do not search
	context of inner classes here.
	(patch_method_invocation):  Search scope, ie. current and outer clases,
	for method matching simple name, to find class.

Index: java-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/java-tree.h,v
retrieving revision 1.97.2.11
diff -u -p -r1.97.2.11 java-tree.h
--- java-tree.h	2001/04/26 18:40:27	1.97.2.11
+++ java-tree.h	2001/04/26 19:23:58
@@ -958,6 +958,7 @@ extern tree lookup_java_constructor PARA
 extern tree lookup_java_method PARAMS ((tree, tree, tree));
 extern tree lookup_argument_method PARAMS ((tree, tree, tree));
 extern tree lookup_argument_method2 PARAMS ((tree, tree, tree));
+extern int has_method PARAMS ((tree, tree));
 extern tree promote_type PARAMS ((tree));
 extern tree get_constant PARAMS ((struct JCF*, int));
 extern tree get_name_constant PARAMS ((struct JCF*, int));
Index: parse.y
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/parse.y,v
retrieving revision 1.251.2.18
diff -u -p -r1.251.2.18 parse.y
--- parse.y	2001/04/20 15:53:11	1.251.2.18
+++ parse.y	2001/04/26 19:24:07
@@ -9897,7 +9897,29 @@ patch_method_invocation (patch, primary,
          alternate class is specified. */
       else
 	{
-	  class_to_search = (where ? where : current_class);
+	  if (where != NULL_TREE)
+	    class_to_search = where;
+	  else if (QUALIFIED_P (name))
+	    class_to_search = current_class;
+	  else
+	    {
+	      class_to_search = current_class;
+
+	      for (;;)
+		{
+		  if (has_method (class_to_search, name))
+		    break;
+		  if (! INNER_CLASS_TYPE_P (class_to_search))
+		    {
+		      parse_error_context (wfl,
+					   "No method named `%s' in scope",
+					   IDENTIFIER_POINTER (name));
+		      PATCH_METHOD_RETURN_ERROR ();
+		    }
+		  class_to_search
+		    = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
+		}
+	    }
 	  lc = 0;
 	}
 
@@ -10512,8 +10534,6 @@ find_applicable_accessible_methods_list 
   /* Search classes */
   else
     {
-      tree sc = class;
-      int seen_inner_class = 0;
       search_applicable_methods_list (lc, TYPE_METHODS (class), 
 				      name, arglist, &list, &all_list);
 
@@ -10530,7 +10550,7 @@ find_applicable_accessible_methods_list 
       /* We must search all interfaces of this class */
       if (!lc)
       {
-	tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
+	tree basetype_vec = TYPE_BINFO_BASETYPES (class);
 	int n = TREE_VEC_LENGTH (basetype_vec), i;
 	for (i = 1; i < n; i++)
 	  {
@@ -10544,24 +10564,6 @@ find_applicable_accessible_methods_list 
 	      }
 	  }
       }
-
-      /* Search enclosing context of inner classes before looking
-         ancestors up. */
-      while (!lc && INNER_CLASS_TYPE_P (class))
-	{
-	  tree rlist;
-	  seen_inner_class = 1;
-	  class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
-	  rlist = find_applicable_accessible_methods_list (lc, class, 
-							   name, arglist);
-	  list = chainon (rlist, list);
-	}
-
-      if (!lc && seen_inner_class 
-	  && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
-	class = CLASSTYPE_SUPER (sc);
-      else
-	class = sc;
 
       /* Search superclass */
       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
Index: typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/typeck.c,v
retrieving revision 1.37.2.2
diff -u -p -r1.37.2.2 typeck.c
--- typeck.c	2001/02/19 20:43:33	1.37.2.2
+++ typeck.c	2001/04/26 19:24:08
@@ -579,6 +579,13 @@ get_type_from_signature (tree signature)
   return type;
 }
 
+tree
+build_null_signature (type)
+     tree type;
+{
+  return NULL_TREE;
+}
+
 /* Return the signature string for the arguments of method type TYPE. */
 
 tree
@@ -761,9 +768,20 @@ lookup_java_method (searched_class, meth
 		    method_signature, build_java_signature);
 }
 
-/* Search in class SEARCHED_CLASS (an its superclasses) for a method
+/* Return true iff CLASS (or its ancestors) has a method METHOD_NAME. */
+
+int
+has_method (class, method_name)
+     tree class;
+     tree method_name;
+{
+  return lookup_do (class, class,  method_name,
+		    NULL_TREE, build_null_signature) != NULL_TREE;
+}
+
+/* Search in class SEARCHED_CLASS (and its superclasses) for a method
    matching METHOD_NAME and signature SIGNATURE.  Also search in
-   SEARCHED_INTERFACE (an its superinterfaces) for a similar match.
+   SEARCHED_INTERFACE (and its superinterfaces) for a similar match.
    Return the matched method DECL or NULL_TREE.  SIGNATURE_BUILDER is
    used on method candidates to build their (sometimes partial)
    signature.  */

-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]