This is the mail archive of the java-discuss@sourceware.cygnus.com 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]

proposed patch for PR 72 (method lookup in interfaces)



PR 72 was still causing me problems.  I tried Anthony's patch from
http://sourceware.cygnus.com/ml/java-discuss/1999-q4/msg00138.html
but that didn't fix the problem for me.

Recall that the problem is that gcj doesn't look in superinterfaces for 
methods in code compiled according to the revised JLS.  Here's the canonical
example that fails to compile with gcj after compiled with jikes:

// compile with jikes according to revised JLS and see gcj fail
// cause it doesn't find "f" in "C"
public class K {
        interface A {
                void f();
        }
        interface B extends A {
        }
        interface C extends B {
	}
        void g(C x) {
                x.f();
        }
}

I fixed lookup_java_method in typeck.c to also search superinterfaces 
and supersuperinterfaces (loading them if necessary) and searching their 
methods.  This did fix the problem for me and didn't seem to have other 
side effects.  Of course, people with more understanding of gcj may want 
to take a look at that patch and presumably simplify it.

Please also let me know if there's something I overlooked that could
cause me problems down the road.  With this patch, I'm happy to report 
that all of Kaffe's class libraries finally compile under gcj!
That is, after compiling the .java files to .class files with jikes
first.

	- Godmar


Here is the patch:

Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/ChangeLog,v
retrieving revision 1.297
diff -u -3 -r1.297 ChangeLog
--- ChangeLog	1999/11/02 07:51:19	1.297
+++ ChangeLog	1999/11/03 22:22:07
@@ -1,3 +1,8 @@
+Wed Nov  3 15:20:02 MST 1999  Godmar Back <gback@cs.utah.edu>
+
+	* typeck.c: (lookup_java_method):  search all inherited
+	interfaces when looking up interface method.
+
 Mon Nov  1 23:42:00 1999  Alexandre Petit-Bianco  <apbianco@cygnus.com>
 
 	* parse.y (method_header:): Issue error message for rule `type
Index: typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/typeck.c,v
retrieving revision 1.19
diff -u -3 -r1.19 typeck.c
--- typeck.c	1999/10/14 17:13:57	1.19
+++ typeck.c	1999/11/03 22:22:08
@@ -743,10 +743,12 @@
    (Contrast lookup_argument_method, which ignores return type.) */
 
 tree
-lookup_java_method (clas, method_name, method_signature)
-     tree clas, method_name, method_signature;
+lookup_java_method (clazz, method_name, method_signature)
+     tree clazz, method_name, method_signature;
 {
   tree method;
+  tree clas = clazz;
+
   while (clas != NULL_TREE)
     {
       for (method = TYPE_METHODS (clas);
@@ -758,6 +760,50 @@
 	    return method;
 	}
       clas = CLASSTYPE_SUPER (clas);
+    }
+
+  /* If this class is an interface class, search its superinterfaces as
+   * well.  A superinterface is not an interface's superclass: a
+   * super interface is implemented by the interface.
+   */
+
+  clas = clazz;
+  if (CLASS_INTERFACE(TYPE_NAME(clas)))
+    {
+      int i;
+      int interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (clas)) - 1;
+
+      for (i = interface_len;  i > 0; i--)
+	{
+	  tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (clas), i);
+	  tree iclass = BINFO_TYPE (child);
+
+	  /* If the superinterface hasn't been loaded yet, do so now.  */
+	  if (! CLASS_LOADED_P (iclass))
+	    load_class (iclass, 1);
+
+	  for (method = TYPE_METHODS (iclass);
+	       method != NULL_TREE;  method = TREE_CHAIN (method))
+	    {
+	      tree method_sig = build_java_signature (TREE_TYPE (method));
+
+	      if (DECL_NAME (method) == method_name 
+		  && method_sig == method_signature)
+		return method;
+	    }
+
+	  /* it could be defined in a supersuperinterface */
+	  if (CLASS_INTERFACE(TYPE_NAME(iclass)))
+	    {
+	      method = lookup_java_method(iclass, 
+	      				  method_name, 
+					  method_signature);
+	      if (method != NULL_TREE) 
+	        {
+	          return method;
+	        }
+	    }
+	}
     }
   return NULL_TREE;
 }

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