This is the mail archive of the
java-discuss@sourceware.cygnus.com
mailing list for the Java project.
Re: initialization of final fields
- To: Godmar Back <gback at cs dot utah dot edu>
- Subject: Re: initialization of final fields
- From: Per Bothner <per at bothner dot com>
- Date: 03 Nov 1999 15:54:23 -0800
- Cc: java-discuss at sourceware dot cygnus dot com, vgp at dms dot at
- References: <199911032257.PAA07193@faith.cs.utah.edu>
Godmar Back <gback@cs.utah.edu> writes:
> My suggestion: until GCJ can prove that an assignment to a final
> variable is indeed illegal, could you make the error in
> expr.c:expand_java_field_op suppressable?
I think we can say that a method may assign to a final field
iff the method is private and is only called (directly or indirectly)
by the initializer.
You can verify this with two extra bits per method
METHOD_CALLED_BY_NON_INIT (the method is called directly or indirectly
by a non-private method wich is not "<init>" or <clinit>"),
and METHOD_SETS_FINAL_FIELD (the method sets a final field, or
it calls directly or indirectly a method which sets a final field).
These are mutually exclusive in correct code, and
only apply to private methods.
The tricky part is to set these during a single scan of the bytecode.
Something like this should work:
If method A sets a final field:
if (METHOD_CALLED_BY_NON_INIT (A)) error(...);
METHOD_SETS_FINAL_FIELD (A) = 1;
If method A calls method B:
oldflag = METHOD_CALLED_BY_NON_INIT (B);
if (METHOD_CALLED_BY_NON_INIT (A)
|| (A is not "<init>" && ! METHOD_PRIVATE (A)))
METHOD_CALLED_BY_NON_INIT (B) = 1;
if (METHOD_SETS_FINAL_FIELD (B))
if (METHOD_CALLED_BY_NON_INIT (B) && ! oldflag) error(...);
METHOD_SETS_FINAL_FIELD (A) = 1;
(Checking oldflag is only to avoid duplicate error messages.)
--
--Per Bothner
per@bothner.com http://www.bothner.com/~per/