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


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

PATCH: java instance initializers



If a java class contains instance initializers called through a
constructor invoking `this', the initializers are executed twice:

public class II {
  {
    System.out.println("ii");
  }
  public II()
  {
  }
  public II(int x)
  {
    this();
  }
  public static void main(String[] args)
  {
    new II(0);
  }
}

$ gcj -C II.java; gij II
ii
ii

The patch below fixes this bug and passes the libgcj testsuite.  OK for
trunk?


2001-07-02  Jeff Sturm  <jsturm@one-point.com>

	* java/parse.y (fix_constructors): Test if a CALL_EXPR invokes
	`this'.  If so, don't build instance initializers.

--- parse.y.orig	Mon Jul  2 22:03:26 2001
+++ parse.y	Mon Jul  2 22:17:29 2001
@@ -8535,6 +8535,7 @@
   else 
     {
       int found = 0;
+      int invokes_this = 0;
       tree found_call = NULL_TREE;
       tree main_block = BLOCK_EXPR_BODY (body);
       tree ii;			/* Instance Initializer */
@@ -8544,6 +8545,9 @@
 	  {
 	  case CALL_EXPR:
 	    found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
+	    if (EXPR_WFL_NODE (TREE_OPERAND (body, 0)) == 
+		  this_identifier_node)
+	      invokes_this = 1;
 	    body = NULL_TREE;
 	    break;
 	  case COMPOUND_EXPR:
@@ -8578,7 +8582,7 @@
 	}
       
       /* Insert the instance initializer block right after. */
-      if ((ii = build_instance_initializer (mdecl)))
+      if (!invokes_this && (ii = build_instance_initializer (mdecl)))
 	compound = add_stmt_to_compound (compound, NULL_TREE, ii);
 
       /* Fix the constructor main block if we're adding extra stmts */



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