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 for ConstantValue handling


I checked in the following patches.  The set_constant_value patch
causes us to reject .class files containg bad ConstantValues
attributes - which are also rejected by Sun's verifier, but which we
generate.  The generate_classfile patch above it causes us to no
longer generate the bad ConstantValue!

This means that the latest compiler will reject some class files
generated by previous versions.  I considered emiting a warning
instead, but since those class files really do break other verifiers,
and I didn't think many people would have old gcj-generated classfiles
hanging around, I didn't see much point.

Note the patch is a work-around for front-end errors.  At least we
now generate valid code for valid Java.  However, the compiler still
silently accepts:
        final static long x = 2.5;

2001-08-30  Per Bothner  <per@bothner.com>

	* jcf-write.c (generate_classfile):  Don't write ConstantValue
	attribute if field is not final, for compatibility with jdk.

	* jcf-write.c (generate_classfile):  Convert ConstantValue values
	to correct type.  Work-around for front-end bug.
	* class.c (set_constant_value):  Error if constant has wrong type.

Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/ChangeLog,v
retrieving revision 1.793
diff -u -p -r1.793 ChangeLog
--- ChangeLog	2001/08/30 19:42:36	1.793
+++ ChangeLog	2001/08/30 20:56:59
@@ -1,5 +1,14 @@
 2001-08-30  Per Bothner  <per@bothner.com>
 
+	* jcf-write.c (generate_classfile):  Don't write ConstantValue
+	attribute if field is not final, for compatibility with jdk.
+
+	* jcf-write.c (generate_classfile):  Convert ConstantValue values
+	to correct type.  Work-around for front-end bug.
+	* class.c (set_constant_value):  Error if constant has wrong type.
+
+2001-08-30  Per Bothner  <per@bothner.com>
+
 	* jcf-dump.c (print_constant):  Fix fencepost error so "Float" and
 	"Double" are printed at verbosity 1.
 
Index: jcf-write.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-write.c,v
retrieving revision 1.85
diff -u -p -r1.85 jcf-write.c
--- jcf-write.c	2001/08/28 22:18:57	1.85
+++ jcf-write.c	2001/08/30 20:57:01
@@ -2884,7 +2884,8 @@ generate_classfile (clas, state)
 			      build_java_signature (TREE_TYPE (part)));
       PUT2(i);
       have_value = DECL_INITIAL (part) != NULL_TREE 
-	&& FIELD_STATIC (part) && CONSTANT_VALUE_P (DECL_INITIAL (part));
+	&& FIELD_STATIC (part) && CONSTANT_VALUE_P (DECL_INITIAL (part))
+	&& FIELD_FINAL (part);
       if (have_value)
 	attr_count++;
 
@@ -2896,6 +2897,8 @@ generate_classfile (clas, state)
 	{
 	  tree init = DECL_INITIAL (part);
 	  static tree ConstantValue_node = NULL_TREE;
+	  // This conversion is a work-around for front-end bug.
+	  init = convert (TREE_TYPE (part), init);
 	  ptr = append_chunk (NULL, 8, state);
 	  if (ConstantValue_node == NULL_TREE)
 	    ConstantValue_node = get_identifier ("ConstantValue");
Index: class.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/class.c,v
retrieving revision 1.109
diff -u -p -r1.109 class.c
--- class.c	2001/08/24 17:24:02	1.109
+++ class.c	2001/08/30 20:57:02
@@ -777,6 +777,12 @@ set_constant_value (field, constant)
   else
     {
       DECL_INITIAL (field) = constant;
+      if (TREE_TYPE (constant) != TREE_TYPE (field)
+	  && ! (TREE_TYPE (constant) == int_type_node
+		&& INTEGRAL_TYPE_P (TREE_TYPE (field))
+		&& TYPE_PRECISION (TREE_TYPE (field)) <= 32))
+	error ("ConstantValue attribute of field '%s' has wrong type",
+	       IDENTIFIER_POINTER (DECL_NAME (field)));
       if (FIELD_FINAL (field))
 	DECL_FIELD_FINAL_IUD (field) = 1;
     }

-- 
	--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]